Completed
Pull Request — gcconnex (#1588)
by
unknown
20:10
created

start.php ➔ notification_logging()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
elgg_register_event_handler('init','system','cp_notifications_init');
4
 
5
6
function cp_notifications_init() {
0 ignored issues
show
Coding Style introduced by
cp_notifications_init uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
7
8
	elgg_register_library('elgg:gc_notification:functions', elgg_get_plugins_path() . 'cp_notifications/lib/functions.php');
9
	elgg_register_css('cp_notifications-css','mod/cp_notifications/css/notifications-table.css');
10
11
	// hide the irrelavent view module from the group
12
	elgg_unextend_view("groups/edit", "group_tools/forms/notifications");
13
14
	elgg_register_plugin_hook_handler('register', 'menu:entity', 'notify_entity_menu_setup', 400);
15
	// since most of the notifications are built within the action file itself, the trigger_plugin_hook was added to respected plugins
16
	elgg_register_plugin_hook_handler('cp_overwrite_notification', 'all', 'cp_overwrite_notification_hook');
17
	elgg_register_plugin_hook_handler('cron', 'daily', 'cp_digest_daily_cron_handler');
18
	elgg_register_plugin_hook_handler('cron', 'weekly', 'cp_digest_weekly_cron_handler', 100);
19
	// hooks and events: intercepts and blocks emails and notifications to be sent out
20
	elgg_register_plugin_hook_handler('email', 'system', 'cpn_email_handler_hook');
21
22
23
	$action_base = elgg_get_plugins_path() . 'cp_notifications/actions/cp_notifications';
24
	elgg_register_action('cp_notify/subscribe', "$action_base/subscribe.php");
25
	elgg_register_action('cp_notify/unsubscribe', "$action_base/unsubscribe.php");
26
	elgg_register_action('user/requestnewpassword', "$action_base/request_new_password.php", 'public');
27
	elgg_register_action('cp_notifications/set_personal_subscription', "$action_base/set_personal_subscription.php");
28
	elgg_register_action('cp_notifications/reset_personal_subscription', "$action_base/reset_personal_subscription.php");
29
    elgg_register_action('cp_notifications/subscribe_users_to_group_content',"$action_base/subscribe_users_to_group_content.php");
30
    elgg_register_action('cp_notifications/undo_subscribe_users_to_group_content',"$action_base/undo_subscribe_users_to_group_content.php");
31
    elgg_register_action('cp_notifications/usersettings/save', elgg_get_plugins_path() . 'cp_notifications/actions/usersettings/save.php');
32
	elgg_register_action('cp_notifications/user_autosubscription',"{$action_base}/user_autosubscription.php");
33
	elgg_register_action('cp_notifications/fix_inconsistent_subscription_script',"{$action_base}/fix_inconsistent_subscription_script.php");
34
  
35
  	elgg_register_action('cp_notifications/fix_forums_subscription',"$actions_base/fix_forums_subscription.php");
36
  
37
	elgg_register_action('useradd',"$action_base/useradd.php",'admin'); // actions/useradd.php (core file)
38
39
	// this plugin must now be placed after "group_tools" plugin
40
	elgg_register_action('group_tools/mail', "{$action_base}/group_mail.php"); 
41
42
43
	// Ajax action files
44
	elgg_register_action('cp_notify/retrieve_group_contents', elgg_get_plugins_path().'cp_notifications/actions/ajax_usersettings/retrieve_group_contents.php'); 
45
	elgg_register_action('cp_notify/retrieve_personal_content', elgg_get_plugins_path().'cp_notifications/actions/ajax_usersettings/retrieve_personal_content.php'); 
46
	elgg_register_action('cp_notify/retrieve_pt_users', elgg_get_plugins_path().'cp_notifications/actions/ajax_usersettings/retrieve_pt_users.php'); 
47
	elgg_register_action('cp_notify/retrieve_messages', elgg_get_plugins_path().'cp_notifications/actions/ajax_usersettings/retrieve_messages.php'); 
48
	elgg_register_action('cp_notify/retrieve_user_info', elgg_get_plugins_path().'cp_notifications/actions/ajax_settings/retrieve_user_info.php'); 
49
50
	// send notifications when the action is sent out
51
	elgg_register_event_handler('create','object','cp_create_notification',900);
52
	elgg_register_event_handler('single_file_upload', 'object', 'cp_create_notification');
53
	elgg_register_event_handler('single_zip_file_upload', 'object', 'cp_create_notification');
54
	elgg_register_event_handler('multi_file_upload', 'object', 'cp_create_notification');
55
56
	elgg_register_event_handler('create','annotation','cp_create_annotation_notification');
57
	elgg_register_event_handler('create', 'membership_request', 'cp_membership_request');
58
59
	// we need to check if the mention plugin is installed and activated because it does notifications differently...
60
	if (elgg_is_active_plugin('mentions')) {
61
		elgg_unregister_event_handler('create', 'object','mentions_notification_handler');
62
		elgg_unregister_event_handler('update', 'annotation','mentions_notification_handler');
63
	}
64
65
    elgg_extend_view("js/elgg", "js/notification"); 
66
    elgg_extend_view("js/elgg", "js/popup");
67
    elgg_extend_view("js/elgg","js/wet4/language_ajax");
68
69
    // remove core notification settings portion of the main settings page
70
    elgg_unextend_view('forms/account/settings', 'core/settings/account/notifications');
71
72
73
	/// "minor save" for contents within groups (basically put the option for all the forms, then filter via URL)
74
	$group_entity = elgg_get_page_owner_entity();
75
	$current_user = elgg_get_logged_in_user_entity();
76
77
78
	if (elgg_is_active_plugin('group_operators'))
79
		elgg_load_library('elgg:group_operators');
80
81
	if ($group_entity instanceof ElggGroup) {
82
83
	// TODO: check to make sure that get_group_operators() is available
84
85
	if (elgg_is_logged_in() && (in_array($current_user, get_group_operators($group_entity)) || elgg_is_admin_user($current_user->getGUID()))) {
86
87
88
			$url = str_replace(elgg_get_site_url(),"", $_SERVER['REQUEST_URI']);
89
			if (strpos($url,'edit') == false) {
90
91
				$plugin_list = elgg_get_plugins('active', 1);
92
				foreach ($plugin_list as $plugin_form)
93
				{
94
					$filepath = elgg_get_plugins_path().$plugin_form['title'].'/views/default/forms/'.$plugin_form['title'];
95
					if (file_exists($filepath))
96
					{
97
						$dir = scandir($filepath);
98
						foreach ($dir as $form_file)
99
						{
100
							if ((strpos($form_file,'save') !== false || strpos($form_file, 'upload') !== false) && (!strstr($form_file, '.old')))
101
							{
102
								$remove_php = explode('.',$form_file);
103
								elgg_extend_view('forms/'.$plugin_form['title'].'/'.$remove_php[0], 'forms/minor_save', 500);
104
							}
105
						}
106
					}
107
				}
108
				
109
				elgg_extend_view('forms/photos/image/save', 'forms/minor_save', 500);
110
				elgg_extend_view('forms/photos/album/save', 'forms/minor_save', 500);
111
				elgg_extend_view('forms/discussion/save', 'forms/minor_save', 500);
112
			}
113
		}
114
	}
115
116
	$subtype_array = array('blog', 'bookmarks', 'discussion');
117
	foreach ($subtype_array as $subtype) 
118
		elgg_register_plugin_hook_handler('action', $subtype.'/save', 'minor_save_hook_handler', 300);
119
	
120
	$subtype_array = array('file/upload', 'ideas/saveidea', 'photos/album/save');
121
	foreach ($subtype_array as $subtype) 
122
		elgg_register_plugin_hook_handler('action', $subtype, 'minor_save_hook_handler', 300);	
123
124
}
125
126
127
/**
128
 * catches the minor save, determines whether to cancel or process the event handlers
129
 *
130
 * @param string $hook    The name of the plugin hook
131
 * @param string $type    The type of the plugin hook
132
 * @param mixed  $value   The current value of the plugin hook
133
 * @param mixed  $params  Data passed from the trigger
134
 *
135
 * @return mixed if not null, this will be the new value of the plugin hook
136
 */
137
function minor_save_hook_handler($hook, $type, $value, $params) {
138
139
140
    if (strcmp(get_input('minor_save'), 'yes') === 0) {
141
142
	    elgg_unregister_event_handler('create','object','cp_create_notification', 900);
143
		elgg_unregister_event_handler('single_file_upload', 'object', 'cp_create_notification');
144
		elgg_unregister_event_handler('single_zip_file_upload', 'object', 'cp_create_notification');
145
		elgg_unregister_event_handler('multi_file_upload', 'object', 'cp_create_notification');
146
		elgg_unregister_event_handler('create','annotation','cp_create_annotation_notification');
147
148
	}
149
150
    return true;
151
}
152
153
154
/**
155
 *
156
 * This contains all the notifications that are required to be triggered from the original action files. Filepaths
157
 * are documented beside each case (and in the readme.md file)
158
 *
159
 * @param string $hook    The name of the plugin hook
160
 * @param string $type    The type of the plugin hook
161
 * @param mixed  $value   The current value of the plugin hook
162
 * @param mixed  $params  Data passed from the trigger
163
 */
164
function cp_overwrite_notification_hook($hook, $type, $value, $params) {
165
166
	elgg_load_library('elgg:gc_notification:functions');
167
	$cp_msg_type = trim($params['cp_msg_type']);
168
	$to_recipients = array();
169
	$email_only = false;
170
	$add_to_sent = false;
171
	$sender_guid = elgg_get_site_entity()->guid;
172
173
	switch ($cp_msg_type) {
174
175
		/// EMAIL NOTIFICATIONS ONLY (password reset, registration, etc)
176
		case 'cp_friend_invite': // invitefriends/actions/invite.php
177
			$message = array(
178
				'cp_msg_type' => $cp_msg_type,
179
				'cp_from_user' => $params['cp_from']->name,
180
				'cp_to_user' => $params['cp_to'],
181
				'cp_join_url' => $params['cp_join_url'],
182
				'cp_msg' => $params['cp_email_msg'],
183
				'_user_e-mail' => $params['cp_to'],
184
			);
185
			$subject = elgg_echo('cp_notify:subject:invite_new_user',array(),'en') . ' | ' . elgg_echo('cp_notify:subject:invite_new_user',array(),'fr');
186
			$template = elgg_view('cp_notifications/email_template', $message);
187
			$site_template = elgg_view('cp_notifications/site_template', $message);
188
			$user_obj = get_user_by_email($params['cp_to']);
189
190
			$result = (elgg_is_active_plugin('phpmailer')) ? phpmailer_send($params['cp_to'], $params['cp_to'], $subject, $template, NULL,true) : mail($params['cp_to'],$subject,$template,cp_get_headers());
191
			return true;
192
193
194
		case 'cp_forgot_password':	// send email notifications only - /wet4/users/action/password
195
			cp_send_new_password_request($params['cp_password_requester']);
196
			return true;
197
198
		case 'cp_group_invite_email':	// group_tools/lib/functions.php (returns user's email, so return after mail is sent out)
199
			$group_name = $params['cp_group_invite']['name'];
200
			if (elgg_is_active_plugin('wet4')) {
201
				$group_name_en = gc_explode_translation($group_name, 'en');
202
				$group_name_fr = gc_explode_translation($group_name, 'fr');
203
			}
204
205
			$subject = elgg_echo('cp_notify:subject:group_invite_email',array($params['cp_inviter']['name'], $group_name_en),'en') . ' | ' . elgg_echo('cp_notify:subject:group_invite_email',array($params['cp_inviter']['name'], $group_name_fr),'fr');
206
			$subject = htmlspecialchars_decode($subject, ENT_QUOTES);
207
208
209
			$message = array(
210
				'cp_email_invited' => $params['cp_invitee'],
211
				'cp_email_invited_by' => $params['cp_inviter'],
212
				'cp_group_invite' => $params['cp_group_invite'],
213
				'cp_invitation_non_user_url' => $params['cp_invitation_nonuser_url'],
214
				'cp_invitation_url' => $params['cp_invitation_url'],
215
				'cp_invitation_code' => $params['cp_invitation_code'],
216
				'cp_invitation_msg' => $params['cp_invitation_msg'],
217
				'cp_msg_type' => $cp_msg_type,
218
				'_user_e-mail' => $params['cp_invitee'],
219
				'group_link' => $params['group_link'],
220
				'cp_user_profile' => $params['cp_user_profile'],
221
			);
222
			$template = elgg_view('cp_notifications/email_template', $message);
223
			$site_template = elgg_view('cp_notifications/site_template', $message);
224
			$user_obj = get_user_by_email($params['cp_invitee']);
225
226
			$result = (elgg_is_active_plugin('phpmailer')) ? phpmailer_send( $params['cp_invitee'], $params['cp_invitee'], $subject, $template, NULL, true ) : mail($params['cp_invitee'],$subject,$template,cp_get_headers());
227
			return true;
228
229
		case 'cp_useradd': // cp_notifications/actions/useradd.php
230
			$message = array(
231
				'cp_msg_type' => $cp_msg_type,
232
				'cp_user_name' => $params['cp_user_name'],
233
				'cp_site_name' => $params['cp_site_name'],
234
				'cp_site_url' => $params['cp_site_url'],
235
				'cp_username' => $params['cp_username'],
236
				'cp_password' => $params['cp_password'],
237
			);
238
			$to_recipients[] = $params['cp_user'];
239
			$subject = elgg_echo('cp_notify:subject:add_new_user',array(),'en') . ' | ' . elgg_echo('cp_notify:subject:add_new_user',array(),'fr');
240
			$email_only = true;
241
			break;
242
243
		case 'cp_validate_user': // uservalidationbyemail/lib/functions.php
244
			$message = array(
245
				'cp_validate_user' => $params['cp_validate_user'],
246
				'cp_validate_url' => $params['cp_validate_url'],
247
				'cp_msg_type' => $cp_msg_type
248
			);
249
			$subject = elgg_echo('cp_notify:subject:validate_user',array($params['cp_validate_user']['email']),'en') . ' | ' . elgg_echo('cp_notify:subject:validate_user',array($params['cp_validate_user']['email']),'fr');
250
			$to_recipients[] = get_user($params['cp_validate_user']['guid']);
251
			$email_only = true;
252
			break;
253
254
		case 'cp_site_msg_type':	// messages/actions/messages/send.php
255
			$add_to_sent = true;
256
			$sender_guid = $params['cp_from']['guid'];
257
			$to_recipients[] = get_user($params['cp_to']['guid']);
258
			$subject = $params['cp_topic_title'];
259
			$message = array(
260
				'cp_msg_title' => $params['cp_topic_title'],
261
				'cp_msg_content' => $params['cp_topic_description'],
262
				'cp_sender' => $params['cp_from']['name'],
263
				'cp_msg_url' => $params['cp_topic_url'],
264
				'cp_msg_type' => 'cp_site_msg_type',
265
			);
266
			break;
267
268
		/// NORMAL NOTIFICATIONS that will send out both email and site notification
269
		case 'cp_wire_share': // thewire_tools/actions/add.php
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
270
271
272
			$message = array(
273
				'cp_msg_type' => $cp_msg_type,
274
				'cp_shared_by' => $params['cp_shared_by'],
275
				'cp_content_reshare' => $params['cp_content_reshared'],
276
				'cp_content' => $params['cp_content'],
277
				'cp_recipient' => $params['cp_recipient'],
278
				'cp_wire_url' => $params['cp_wire_url'],
279
			);
280
281
			$parent_item = $params['cp_content']->getContainerEntity();
282
283
			$subject = elgg_echo('cp_notify:wireshare:subject',array($params['cp_shared_by']->name),'en').' | ';
284
			$subject .= elgg_echo('cp_notify:wireshare:subject',array($params['cp_shared_by']->name),'fr');
285
286
			$to_recipients[] = $params['cp_recipient'];
287
288
			$content_entity = $params['cp_content_reshared'];
289
			$author = $params['cp_shared_by'];
290
			$content_url = $params['cp_content_reshared']->getURL();
291
			break;
292
293
294
		case 'cp_messageboard': // messageboard/actions/add.php
295
			$message = array(
296
				'cp_msg_type' => $cp_msg_type,
297
				'cp_message_content' => $params['cp_message_content'],
298
				'cp_writer_name' => $params['cp_writer']->name,
299
				'cp_owner_profile' => $params['cp_recipient']->getURL(),
300
			);
301
			$subject = elgg_echo('cp_notify:messageboard:subject',array(),'en') . ' | ' . elgg_echo('cp_notify:messageboard:subject',array(),'fr');
302
			$to_recipients[] = $params['cp_recipient'];
303
304
			$content_entity = $params['cp_message_content'];
305
			$author = $params['cp_writer'];
306
			break;
307
308
		case 'cp_add_grp_operator': // group_operators/actions/group_operators/add.php (adds group operator)
309
			$message = array(
310
				'cp_msg_type' => $cp_msg_type,
311
				'cp_to_operator' => $params['cp_to_operator'],
312
				'cp_who_made_operator' => $params['cp_who_made_operator'],
313
				'cp_group_name' => $params['cp_group_name'],
314
				'cp_who_made_operator' => $params['cp_who_made_operator'],
315
				'cp_group_url' => $params['cp_group_url'],
316
			);
317
			$subject = elgg_echo('cp_notify:subject:add_grp_operator',array(gc_explode_translation($params['cp_group_name'], 'en')),'en') . ' | ' . elgg_echo('cp_notify:subject:add_grp_operator',array(gc_explode_translation($params['cp_group_name'], 'fr')),'fr');
318
			$to_recipients[] = $params['cp_to_user'];
319
			break;
320
321
322
		case 'cp_grp_admin_transfer': // group_tools/lib/functions.php (this is transfer of group owner through group edit)
323
			$message = array(
324
				'cp_msg_type' => $cp_msg_type,
325
				'cp_group_name' => $params['cp_group_name'],
326
				'cp_group_url' => $params['cp_group_url'],
327
				'cp_appointer' => $params['cp_appointer']
328
			);
329
			$subject = elgg_echo('cp_notify:subject:group_admin_transfer',array(gc_explode_translation($params['cp_group_name'],'en')),'en') . ' | ' . elgg_echo('cp_notify:subject:group_admin_transfer',array(gc_explode_translation($params['cp_group_name'],'fr')),'fr');
330
			$to_recipients[] = $params['cp_new_owner_user'];
331
			break;
332
333
334
		case 'cp_wire_mention': // thewire_tools/lib/events.php (TODO: share option in notifications setting)
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
335
336
			$message = array(
337
				'cp_mention_by' => $params['cp_mention_by'],
338
				'cp_view_mention' => $params['cp_view_your_mention'],
339
				'cp_msg_type' => $cp_msg_type,
340
				'cp_wire_mention_url' => $params['cp_wire_mention_url'],
341
			);
342
343
			$subject = elgg_echo('cp_notify:subject:wire_mention',array($params['cp_mention_by']),'en') . ' | ' . elgg_echo('cp_notify:subject:wire_mention',array($params['cp_mention_by']),'fr');
344
			$to_recipients[] = $params['cp_send_to'];
345
346
			$content_entity = $params['cp_wire_entity'];
347
			$author = $content_entity->getOwnerEntity();
348
			break;
349
350
351
		case 'cp_friend_approve': // friend_request/actions/approve
352
			$subject = elgg_echo('cp_notify:subject:approve_friend',array($params['cp_approver']),'en') . ' | ' . elgg_echo('cp_notify:subject:approve_friend',array($params['cp_approver']),'fr');
353
			$message = array(
354
				'cp_approver' => $params['cp_approver'],
355
				'cp_approver_profile' => $params['cp_approver_profile'],
356
				'cp_msg_type' => $cp_msg_type,
357
358
				);
359
			$to_recipients[] = get_user($params['cp_request_guid']);
360
361
			$content_entity = $params['object'];
362
			$author = $params['object'];
363
			break;
364
365
366
		case 'cp_group_add':	// group_tools/lib/functions.php OR groups/actions/groups/membership/add.php ????
367
			$to_recipients[] = $params['cp_user_added'];
368
			$subject = elgg_echo('cp_notify:subject:group_add_user',array(gc_explode_translation($params['cp_group']['name'],'en')),'en') . ' | ' . elgg_echo('cp_notify:subject:group_add_user',array(gc_explode_translation($params['cp_group']['name'],'fr')),'fr');
369
			$message = array(
370
				'cp_user_added' => $params['cp_user_added'],
371
				'cp_group' => $params['cp_group'],
372
				'cp_message' => $params['cp_added_msg'],
373
				'cp_msg_type' => $cp_msg_type
374
			);
375
			break;
376
377
378
		case 'cp_group_invite': // group_tools/lib/functions.php
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
379
380
			$subject = elgg_echo('cp_notify:subject:group_invite_user',array($params['cp_inviter']['name'],gc_explode_translation($params['cp_invite_to_group']['name'],'en')),'en');
381
			$subject .= ' | '.elgg_echo('cp_notify:subject:group_invite_user',array($params['cp_inviter']['name'],gc_explode_translation($params['cp_invite_to_group']['name'],'fr')),'fr');
382
383
			$message = array(
384
				'cp_group_invite_from' => $params['cp_invitee'], // user we're inviting
385
				'cp_group_invite_to' => $params['cp_inviter'], // user inviting others
386
				'cp_group' => $params['cp_invite_to_group'],
387
				'cp_invitation_url' => $params['cp_invitation_url'],
388
				'cp_invitation_msg' => $params['cp_invite_msg'],
389
				'cp_msg_type' => $cp_msg_type
390
			);
391
			$to_recipients[] = get_user($params['cp_invitee']['guid']);
392
393
			$content_entity = $params['cp_invite_to_group'];
394
			$author = $params['cp_inviter'];
395
			break;
396
397
398
		case 'cp_group_mail': // group_tools/actions/mail.php
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
399
		
400
			$message = array(
401
				'cp_group' => $params['cp_group'],
402
				'cp_group_subject' => $params['cp_group_subject'],
403
				'cp_group_message' => $params['cp_group_message'],
404
				'cp_msg_type' => $cp_msg_type
405
			);
406
			$subject = elgg_echo('cp_notify:subject:group_mail',array($params['cp_group_subject'],gc_explode_translation($params['cp_group']['name'],'en')),'en'). ' | ' . elgg_echo('cp_notify:subject:group_mail',array($params['cp_group_subject'],gc_explode_translation($params['cp_group']['name'],'fr')),'fr');
407
			foreach ($params['cp_group_mail_users'] as $to_user) {
408
				$to_recipients[$to_user] = get_user($to_user);
409
				
410
			}
411
			break;
412
413
414
		case 'cp_friend_request': // friend_request/lib/events.php
415
			$message = array(
416
				'cp_friend_request_from' => $params['cp_friend_requester'],
417
				'cp_friend_request_to' => $params['cp_friend_receiver'],
418
				'cp_friend_invitation_url' => $params['cp_friend_invite_url'],
419
				'cp_msg_type' => $cp_msg_type
420
			);
421
			$subject = elgg_echo('cp_notify:subject:friend_request',array($params['cp_friend_requester']['name']),'en') . ' | ' . elgg_echo('cp_notify:subject:friend_request',array($params['cp_friend_requester']['name']),'fr');
422
			$to_recipients[] = get_user($params['cp_friend_receiver']['guid']);
423
424
			$content_entity = $params['cp_relationship'];
425
			$author = $params['cp_friend_requester'];
426
427
			break;
428
429
430
		case 'cp_hjpost': // gcforums/actions/gcforums/create.php
431
			$message = array(
432
				'cp_hjpost_author' => $params['cp_topic_author'],
433
				'cp_hjpost_title' => $params['cp_topic_title'],
434
				'cp_hjpost_description' => $params['cp_topic_description'],
435
				'cp_hjpost_url' => $params['cp_topic_url'],
436
				'cp_msg_type' => $cp_msg_type
437
				);
438
			$t_user = $params['cp_subscribers'];
439
			$subject = elgg_echo('cp_notify:subject:hjpost',array($params['cp_topic_author'],$params['cp_topic_title']),'en');
440
			$subject .= ' | '.elgg_echo('cp_notify:subject:hjpost',array($params['cp_topic_author'],$params['cp_topic_title']),'fr');
441
			foreach ($t_user as $s_uer)
442
				$to_recipients[] = get_user($s_uer);
443
444
			$content_entity = $params['cp_post'];
445
			$content_url = $params['cp_topic_url'];
446
			$author = $params['cp_post']->getOwnerEntity();
447
448
			break;
449
450
451
		case 'cp_hjtopic': // gcforums/actions/gcforums/create.php
452
			$message = array(
453
				'cp_hjtopic_author' => $params['cp_topic_author'],
454
				'cp_hjtopic_title' => $params['cp_topic_title'],
455
				'cp_hjtopic_description' => $params['cp_topic_description'],
456
				'cp_hjtopic_url' => $params['cp_topic_url'],
457
				'cp_msg_type' => $cp_msg_type
458
				);
459
			$t_user = $params['cp_subscribers'];
460
			$subject = elgg_echo('cp_notify:subject:hjtopic',array($params['cp_topic_author'],$params['cp_topic_title']),'en');
461
			$subject .= ' | '.elgg_echo('cp_notify:subject:hjtopic',array($params['cp_topic_author'],$params['cp_topic_title']),'fr');
462
			foreach ($t_user as $s_uer)
463
				$to_recipients[] = get_user($s_uer);
464
465
			$content_url = $params['cp_topic_url'];
466
			$content_entity = $params['cp_topic'];
467
			$author = $params['cp_topic']->getOwnerEntity();
468
			break;
469
470
		case 'cp_event_request': // event_calendar/actions/event_calendar/request_personal_calendar.php
471
			$message = array(
472
				'cp_event_request_user' => $params['cp_event_request_user'],
473
				'cp_event_request_url' => $params['cp_event_request_url'],
474
				'cp_event_object' => $params['cp_event_obj'],
475
				'type_event' => $params['type_event'], // create, request, cancel
476
				'cp_msg_type' => $cp_msg_type
477
			);
478
			$subject = elgg_echo('cp_notify:event_request:subject',array($params['cp_event_request_user'], $params['cp_event_obj']->title),'en');
479
			$subject .= ' | '.elgg_echo('cp_notify:event_request:subject',array($params['cp_event_request_user'], $params['cp_event_obj']->title),'fr');
480
			$to_recipients[] = $params['cp_event_owner'];
481
			break;
482
483
		case 'cp_event_ics': // .../mod/event_calendar/actions/event_calendar/add_ics.php
484
			$message = array(
485
				'cp_event_send_to_user' => $params['cp_event_send_to_user'],
486
				//'cp_event_invite_url' => $params['cp_event_invite_url'],
487
				'startdate' => $params['startdate'],
488
				'enddate' => $params['enddate'],
489
				'cp_event' => $params['cp_event'],
490
				'cp_msg_type' => $cp_msg_type,
491
			);
492
493
			$event = $params['cp_event'];
494
			$startdate = $params['startdate'];
495
			$enddate = $params['enddate'];
496
			
497
			// Add to my Outlook calendar | Ajoutez a mon calendrier d'Outlook
498
		    $subject = $event->title.' - '.elgg_get_logged_in_user_entity()->username; 
499
500
		   	$event = 'event';
501
		   	$to_recipients[] = $params['cp_event_send_to_user'];
502
			break;
503
504
		default:
505
			break;
506
	}
507
508
509
	if (empty($subject))
510
		return false;
511
512
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
513
514
	if (is_array($to_recipients)) {
515
		foreach ($to_recipients as $to_recipient) {
516
			// username for link in footer (both email notification and site notification
517
			$message['user_name'] = $to_recipient->username;
518
			if ($cp_msg_type != 'cp_event_ics') {
519
				$template = elgg_view('cp_notifications/email_template', $message);
520
				$site_template = elgg_view('cp_notifications/site_template', $message);
521
			}
522
523
			$newsletter_appropriate = array('cp_wire_share','cp_messageboard','cp_wire_mention','cp_hjpost','cp_hjtopic', 'cp_friend_request', 'cp_friend_approve');
524
			if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid,'cp_notifications'),'set_digest_yes') == 0 && in_array($cp_msg_type, $newsletter_appropriate)) {
525
				$result = create_digest($author, $cp_msg_type, $content_entity, $to_recipient, $content_url);
526
				continue;
527
528
			} else {
529
				
530
				$result = (elgg_is_active_plugin('phpmailer')) ? phpmailer_send( $to_recipient->email, $to_recipient->name, $subject, $template ) : mail($to_recipient->email, $subject, $template, cp_get_headers($event));
531
			}
532
533
			if (!$email_only)
534
				messages_send($subject, $site_template, $to_recipient->guid, $sender_guid, 0, true, $add_to_sent);
535
		}
536
	}
537
538
	// register the error, if either of the arrays are not populated
539
	if (!is_array($to_recipients)) {
540
		notification_logging('error: in cp_create_notification(), $to_recipients is not array');
541
	}
542
}
543
544
545
/**
546
 * returns the headers for ical
547
 *
548
 * @param string 		$type_event
549
 * @param ElggObject 	$event
550
 * @param string 		$start_date
551
 * @param string 		$end_date
552
 */
553
function cp_ical_headers($event_type, $event, $start_date, $end_date) {
554
555
	$end_date = date("Ymd\THis", strtotime($end_date));
556
	$start_date = date("Ymd\THis", strtotime($startdate));
557
	$current_date = date("Ymd\TGis");
558
559
	$ical = "
560
	BEGIN:VCALENDAR \r\n
561
    PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN \r\n
562
    VERSION:2.0 \r\n
563
    METHOD: {$type_event} \r\n
564
    BEGIN:VTIMEZONE \r\n
565
    TZID:Eastern Time \r\n
566
    BEGIN:STANDARD \r\n
567
    DTSTART:20091101T020000 \r\n
568
    RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11 \r\n
569
    TZOFFSETFROM:-0400 \r\n
570
    TZOFFSETTO:-0500 \r\n
571
    TZNAME:EST \r\n
572
    END:STANDARD \r\n
573
    BEGIN:DAYLIGHT \r\n
574
    DTSTART:20090301T020000 \r\n
575
    RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3 \r\n
576
    TZOFFSETFROM:-0500 \r\n
577
    TZOFFSETTO:-0400 \r\n
578
    TZNAME:EDST \r\n
579
    END:DAYLIGHT \r\n
580
    END:VTIMEZONE \r\n
581
    BEGIN:VEVENT \r\n
582
    LAST-MODIFIED: {$current_date} \r\n
583
    UID: {$event->guid} \r\n
584
    DTSTAMP:  \r\n
585
    DTSTART;TZID='Eastern Time': {$start_date} \r\n
586
    DTEND;TZID='Eastern Time': {$end_date} \r\n
587
    TRANSP:OPAQUE \r\n
588
    SEQUENCE:1 \r\n
589
	SUMMARY: {$event->title} \r\n
590
	LOCATION: {$event->venue} \r\n
591
    CLASS:PUBLIC \r\n
592
    PRIORITY:5 \r\n
593
    BEGIN:VALARM \r\n
594
    TRIGGER:-PT15M \r\n
595
    ACTION:DISPLAY \r\n
596
    DESCRIPTION:Reminder \r\n
597
    END:VALARM \r\n
598
    END:VEVENT \r\n
599
    END:VCALENDAR \r\n";
600
601
	return $ical;
602
}
603
604
605
606
607
/**
608
 * cp_create_annotation_notification is an event handler, invokes everytime a user likes something, edit something, etc
609
 *
610
 * This contains the likes and the comments that get posted. we also filter out the
611
 * following : blog revision, discussion replies (?), tasks, poll votes, folder creation
612
 *
613
 * @param string $event		the name of the event
614
 * @param string $type		the type of the object
615
 * @param mixed $object		the object/entity of the event
616
 */
617
function cp_create_annotation_notification($event, $type, $object) {
618
619
	elgg_load_library('elgg:gc_notification:functions');
620
	$entity = get_entity($object->entity_guid);
621
622
	if ($entity->entity_minor_edit)	return;
623
624
	$dbprefix = elgg_get_config('dbprefix');
625
	$site = elgg_get_site_entity();
626
	$object_subtype = $object->getSubtype();
627
	$liked_content = get_entity($object->entity_guid);
628
	$type_of_like = $liked_content->getSubtype();
629
	if (!$type_of_like) $type_of_like = $liked_content->getType();
630
631
	$action_type = "content_revision";
632
	$author = $liked_by;
633
634
	/// EDITS TO BLOGS AND PAGES, THEY ARE CONSIDERED ANNOTATION DUE TO REVISIONS AND MULTIPLE COPIES OF SAME CONTENT
635
	if (strcmp($object_subtype, 'likes') != 0) {
636
637
		$content = get_entity($object->entity_guid);
638
639
		// auto save -drafts or -published blogs, we don't send out notifications
640
		if (strcmp($object_subtype,'blog_auto_save') == 0 && (strcmp($entity->status,'draft') == 0 || strcmp($entity->status, 'published') == 0)) return;
641
642
643
		// if we are publishing, or revising blogs then send out notification
644
		if (strcmp($object_subtype,'blog_revision') == 0 && strcmp($entity->status,'published') == 0) {
645
			$current_user = get_user($entity->getOwnerGUID());
646
			$subject = elgg_echo('cp_notify:subject:edit_content',array('The blog',gc_explode_translation($entity->title,'en'), $current_user->username),'en') . ' | ' . elgg_echo('cp_notify:subject:edit_content:m',array('Le blogue',gc_explode_translation($entity->title,'fr'), $current_user->username),'fr');
647
			
648
			$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
649
650
			$message = array(
651
				'cp_content' => $entity,
652
				'cp_user' => $current_user->username,
653
				'cp_msg_type' => 'cp_content_edit',
654
				'cp_fr_entity' => 'Ce blogue',
655
				'cp_en_entity' => 'blog',
656
			);
657
658
			$author = $current_user;
659
			$content_entity = $entity;
660
661
			$watchers = get_subscribers($dbprefix, $current_user->guid, $entity->guid);
662
663
			foreach ($watchers as $watcher) {
664
				$message['user_name'] = $watcher->username;
665
666
				$template = elgg_view('cp_notifications/email_template', $message);
667
668
				$recipient_user = get_user($watcher->guid);
669
670
		
671
				if (has_access_to_entity($entity, $recipient_user) && $object->access_id != 0) {
672
673
					if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $watcher->guid,'cp_notifications'), 'set_digest_yes') == 0)
674
						create_digest($author, $action_type, $content_entity, get_entity($watcher->guid));
675
					else
676
						(elgg_is_active_plugin('phpmailer')) ? phpmailer_send($watcher->email, $watcher->name, $subject, $template, NULL, true) : mail($watcher->email, $subject, $template, cp_get_headers());
677
				
678
					if (check_entity_relationship($watcher->guid, 'cp_subscribed_to_site_mail', $entity->getContainerGUID()))
679
						messages_send($subject, $template, $watcher->guid, $site->guid, 0, true, false);
680
				}
681
682
				return true;
683
			}
684
		}
685
686
687
		// checks for condition if the content being modified is a page or task
688
		if (strcmp($object_subtype,'page') == 0 || strcmp($object_subtype,'page_top') == 0 || strcmp($object_subtype,'task') == 0 || strcmp($object_subtype,'task_top') == 0) {
689
			$current_user = get_user($object->owner_guid);
690
			$subject = elgg_echo('cp_notify:subject:edit_content',array('The page', $entity->title, $current_user->username),'en');
691
			$subject .= ' | '.elgg_echo('cp_notify:subject:edit_content:f',array('La page',$entity->title, $current_user->username),'fr');
692
693
			$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
694
695
			$message = array(
696
				'cp_content' => $entity,
697
				'cp_user' => $current_user->username,
698
				'cp_msg_type' => 'cp_content_edit',
699
				'cp_fr_entity' => 'Cette page',
700
				'cp_en_entity' => 'page',
701
			);
702
703
			$author = $current_user;
704
			$content_entity = $entity;
705
706
			$watchers = get_subscribers($dbprefix, $current_user->guid, $entity->guid);
707
708
			foreach ($watchers as $watcher) {
709
				$message['user_name'] = $watcher->username;
710
711
				$template = elgg_view('cp_notifications/email_template', $message);
712
				$recipient_user = get_user($watcher->guid);
713
714
				if (has_access_to_entity($entity, $recipient_user) $object->access_id != 0) {
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 T_VARIABLE
Loading history...
715
716
					if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $watcher->guid,'cp_notifications'),'set_digest_yes') == 0)
717
						create_digest($author, $action_type, $content_entity, get_entity($watcher->guid));
718
					else
719
						(elgg_is_active_plugin('phpmailer')) ? phpmailer_send( $watcher->email, $watcher->name, $subject, $template, NULL, true ) : mail($watcher->email, $subject, $template, cp_get_headers());
720
721
					if (check_entity_relationship($watcher->guid, 'cp_subscribed_to_site_mail', $entity->getContainerGUID())) {
722
						$site_template = elgg_view('cp_notifications/site_template', $message);
723
						messages_send($subject, $site_template, $watcher->guid, $site->guid, 0, true, false);
724
					}
725
				}
726
			
727
				return true;
728
			}
729
		}
730
		
731
	} else {
732
733
		/// LIKES TO COMMENTS AND DISCUSSION REPLIES
734
    	$content_entity = get_entity($object->entity_guid); 			// get the comment object
735
		$comment_author = get_user($content_entity->owner_guid); 		// get the user who made the comment
736
		$content = get_entity($content_entity->getContainerGUID());		// get the location of comment
737
		$content_title = $content->title; 									// get title of content
738
		$liked_by = get_user($object->owner_guid); 							// get user who liked comment
739
740
		$action_type = "post_likes";
741
742
		$to_recipients = array();
743
		$to_recipients_site = array();
744
745
	    switch ($type_of_like) {
746
	    	case 'group':
747
	    		$group_name_en = gc_explode_translation($content_entity->name, 'en');
748
	    		$group_name_fr = gc_explode_translation($content_entity->name, 'fr');
749
750
	    		$subject = elgg_echo(elgg_echo('cp_notify:subject:likes_group', array($liked_by->name, $group_name_en), 'en'));
751
	    		$subject .= elgg_echo(elgg_echo('cp_notify:subject:likes_group', array($liked_by->name, $group_name_fr), 'fr'));
752
753
	    		$message = array(
754
	    			'cp_liked_by' => $liked_by->name,
755
	    			'cp_group' => $content_entity->name,
756
	    			'cp_group_link' => $content_entity->getURL(),
757
	    			'cp_msg_type' => 'cp_like_group'
758
	    		);
759
760
	    		$group_owner = $content_entity->getOwnerEntity();
761
	    		$action_type = 'like_group';
762
763
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $group_owner->getGUID(),'cp_notifications'),'likes_email') == 0)
764
    				$to_recipients[$group_owner->getGUID()] = $group_owner;
765
766
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $group_owner->getGUID(),'cp_notifications'),'likes_site') == 0)
767
    				$to_recipients_site[$group_owner->getGUID()] = $group_owner;
768
769
	    		break;
770
771
	    	case 'comment':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
772
773
	    		$content_title_en = gc_explode_translation($content_title, 'en');
774
	    		$content_title_fr = gc_explode_translation($content_title, 'fr');
775
776
	    		$subject = elgg_echo('cp_notify:subject:likes_comment', array($liked_by->name, $content_title_en),'en');
777
	    		$subject .= ' | '.elgg_echo('cp_notify:subject:likes_comment',array($liked_by->name, $content_title_fr),'fr');
778
779
	    		$message = array(
780
	    			'cp_liked_by' => $liked_by->name,
781
	    			'cp_comment_from' => $content_title,
782
	    			'content_url' => $content->getURL(),
783
					'cp_msg_type' => 'cp_likes_comments',
784
				);
785
786
	    		$author = $liked_by;
787
	    		$action_type = "like_comment";
788
789
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $comment_author->getGUID(),'cp_notifications'),'likes_email') == 0)
790
    				$to_recipients[$comment_author->getGUID()] = $comment_author;
791
792
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $comment_author->getGUID(),'cp_notifications'),'likes_site') == 0)
793
    				$to_recipients_site[$comment_author->getGUID()] = $comment_author;
794
	    		break;
795
796
	    	case 'discussion_reply':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
797
798
	    		$content_title_en = gc_explode_translation($content_title, 'en');
799
	    		$content_title_fr = gc_explode_translation($content_title, 'fr');
800
801
	    		$subject = elgg_echo('cp_notify:subject:likes_discussion',array($liked_by->name, $content_title_en),'en');
802
	    		$subject .= ' | '.elgg_echo('cp_notify:subject:likes_discussion',array($liked_by->name, $content_title_fr),'fr');
803
804
				$message = array(
805
					'cp_liked_by' => $liked_by->name,
806
					'cp_comment_from' => $content_title,
807
					'content_url' => $content->getURL(),
808
					'cp_msg_type' => 'cp_likes_topic_replies',
809
				);
810
				$author = $liked_by;
811
				$action_type = "like_reply";
812
813
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $comment_author->getGUID(),'cp_notifications'),'likes_email') == 0)
814
    				$to_recipients[$comment_author->getGUID()] = $comment_author;
815
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $comment_author->getGUID(),'cp_notifications'),'likes_site') == 0)
816
    				$to_recipients_site[$comment_author->getGUID()] = $comment_author;
817
	    		break;
818
819
820
	    	default:
821
	    		$type_of_like = 'user_update';
822
		    	if ($liked_content instanceof ElggUser) {
823
824
					// cyu - there doesn't seem to be any differentiation between updated avatar and colleague connection
825
		    		$liked_by = get_user($object->owner_guid); // get user who liked comment
826
827
828
		    		$subject = elgg_echo('cp_notify:subject:likes_user_update',array($liked_by->name),'en') . ' | ' . elgg_echo('cp_notify:subject:likes_user_update',array($liked_by->name),'fr');
829
		    		$message = array(
830
						'cp_msg_type' => 'cp_likes_user_update',
831
						'cp_liked_by' => $liked_by->name
832
					);
833
	    			$to_recipients[$liked_content->guid] = $liked_content;
834
835
836
		    	} else {
837
838
		    		$type_of_like = 'content';
839
		    		$liked_by = get_user($object->owner_guid); // get user who liked content
840
		    		$content = get_entity($object->entity_guid);
841
842
		    		$content_entity = $content;
843
		    		$author = $liked_by;
844
845
		    		$content_title_en = gc_explode_translation($content->title, 'en');
846
		    		$content_title_fr = gc_explode_translation($content->title, 'fr');
847
848
		    		// cyu - patching issue #323 (liking wire post)
849
		    		if ($content->getSubtype() === 'thewire') {
850
		    			$subject = elgg_echo('cp_notify:subject:likes_wire',array($liked_by->name, $content_title_en),'en') . ' | ' . elgg_echo('cp_notify:subject:likes_wire',array($liked_by->name, $content_title_fr), 'fr');
851
		    			$content_subtype = 'thewire';
852
853
		    		} else {
854
		    			$subject = elgg_echo('cp_notify:subject:likes',array($liked_by->name, $content_title_en),'en') . ' | ' . elgg_echo('cp_notify:subject:likes',array($liked_by->name, $content_title_fr), 'fr');
855
		    			$content_subtype = '';
856
		    		}
857
858
		    		$message = array(
859
		    			'cp_subtype' => $content_subtype,
860
						'cp_msg_type' => 'cp_likes_type',
861
						'cp_liked_by' => $liked_by->name,
862
						'cp_comment_from' => $content->title,
863
						'cp_description' => $content->description,
864
						'cp_content_url' => $content->getURL(),
865
					);
866
867
		    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $content->getOwnerGUID(),'cp_notifications'), 'likes_email') == 0)
868
	    				$to_recipients[$content->getOwnerGUID()] = $content->getOwnerEntity();
869
870
	    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $content->getOwnerGUID(),'cp_notifications'), 'likes_site') == 0)
871
	    				$to_recipients_site[$content->getOwnerGUID()] = $content->getOwnerEntity();
872
		    	}
873
	    		break;
874
875
		} // end switch statement
876
	}
877
878
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
879
880
	if (is_array($to_recipients)) {
881
		// send notification out via email
882
		foreach ($to_recipients as $to_recipient_id => $to_recipient) {
883
		
884
			$message['user_name'] = get_user($to_recipient->guid)->username;
885
886
			$recipient_user = get_user($to_recipient->guid);
887
888
			if ($liked_by->guid == $entity->getOwnerGUID() && $to_recipient->guid == $liked_by->guid)
889
				continue;
890
891
			if ($object->access_id == 1 || $object->access_id == 2 || $content_entity->getType() === 'group' || $action_type === 'post_likes') {
892
893
				if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid,'cp_notifications'),'set_digest_yes') == 0)
894
					create_digest($author, $action_type, $content_entity, $to_recipient);
895
896
				else {
897
					$template = elgg_view('cp_notifications/email_template', $message);
898
899
					if (elgg_is_active_plugin('phpmailer')) {
900
901
						phpmailer_send( $to_recipient->email, $to_recipient->name, $subject, $template, NULL, true );
902
					}
903
					else
904
						mail($to_recipient->email, $subject, $template, cp_get_headers());
905
				}
906
			}
907
		}
908
	}
909
910
	if (is_array($to_recipients_site)) {
911
		// send notification out via site
912
		foreach ($to_recipients_site as $to_recipient_id => $to_recipient) {
913
			$site_template = elgg_view('cp_notifications/site_template', $message);
914
			$recipient_user = get_user($to_recipient->guid);
915
916
			if (($object->access_id == 1 || $object->access_id == 2 || $content_entity->getType() === 'group' || $action_type === 'post_likes')  && 
917
				(strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid,'cp_notifications'),'set_digest_yes') !== 0)) {
918
919
				messages_send($subject, $site_template, $to_recipient->guid, $site->guid, 0, true, false);
920
			}
921
922
		}
923
	}
924
925
	// register the error, if either of the arrays are not populated
926
	if (!is_array($to_recipients) || !is_array($to_recipients_site)) {
927
		notification_logging('error: in cp_create_notification(), $to_recipients or $to_recipients_site is not array');
928
	}
929
930
} // end of function
931
932
933
934
/**
935
 * function cp_create_notification is an event handler, invokes everytime a new entity is created
936
 * This contains the notifications for new content posted on GCconnex
937
 *
938
 * @param string $event		the name of the event
939
 * @param string $type		the type of object (eg "user", "group", ...)
940
 * @param mixed $object		the object/entity of the event
941
 */
942
function cp_create_notification($event, $type, $object) {
943
944
	$do_not_subscribe_list = array('mission-posted', 'file', 'tidypics_batch', 'hjforum', 'hjforumcategory','hjforumtopic', 'messages', 'hjforumpost', 'site_notification', 'poll_choice','blog_revision','widget','folder','c_photo', 'cp_digest','MySkill', 'education', 'experience', 'poll_choice3');
945
946
	// since we implemented the multi file upload, each file uploaded will invoke this hook once to many times (we don't allow subtype file to go through, but check the event)
947
	if ($object instanceof ElggObject && $event !== 'single_file_upload') {
948
949
		if (in_array($object->getSubtype(), $do_not_subscribe_list)) 
950
			return true;		
951
	} else {
952
953
		if ('single_zip_file_upload' !== $event && 'multi_file_upload' !== $event && 'single_file_upload' !== $event)
954
			return true;
955
	}
956
957
	elgg_load_library('elgg:gc_notification:functions');
958
	$dbprefix = elgg_get_config('dbprefix');
959
	$no_notification = false;
960
	$site = elgg_get_site_entity();
961
	$to_recipients = array();
962
	$subject = "";
963
964
	$switch_case = $event;
965
	if ($object instanceof ElggObject)
966
		$switch_case = $object->getSubtype();	
967
968
969
970
	switch ($switch_case) {
971
972
		/// invoked when zipped file upload function is used (files_tools/lib/functions.php)
973
		case 'single_zip_file_upload':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
974
975
			$entity = get_entity($object['forward_guid']);
976
			if (elgg_instanceof('group', $entity)) {
977
				$file_forward_url = elgg_get_site_entity()->getURL()."file/group/{$object['forward_guid']}/all";
978
			} elseif (elgg_instanceof('user', $entity)) {
979
				$file_forward_url = elgg_get_site_entity()->getURL()."file/owner/{$entity->username}";
980
			} else {
981
				$file_forward_url = $entity->getURL();
982
			}
983
984
			$to_recipients = get_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
985
			$to_recipients_site = get_site_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
986
987
			$subject = elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'file'), 'en');
988
			$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'fichier', false), 'fr');
989
990
			$author = elgg_get_logged_in_user_entity();
991
			$message = array(
992
				'cp_topic' => $entity,
993
				'cp_msg_type' => 'zipped_file',
994
				'files_uploaded' => $object['files_uploaded'],
995
				'cp_topic_description_discussion' => 'Please view the files here',
996
				'cp_topic_description_discussion2' => 'SVP voir les fichiers ici',
997
			);
998
999
			
1000
			$content_entity = $object['files_uploaded'];
1001
			$object = get_entity($object['files_uploaded'][0]);
1002
			$author = elgg_get_logged_in_user_entity();
1003
			break;
1004
1005
		/// invoked when multiple file upload function is used
1006
		case 'multi_file_upload':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1007
1008
			$entity = get_entity($object['forward_guid']);
1009
			if (elgg_instanceof('group', $entity)) {
1010
				$file_forward_url = elgg_get_site_entity()->getURL()."file/group/{$object['forward_guid']}/all";
1011
			} elseif (elgg_instanceof('user', $entity)) {
1012
				$file_forward_url = elgg_get_site_entity()->getURL()."file/owner/{$entity->username}";
1013
			} else {
1014
				$file_forward_url = $entity->getURL();
1015
			}
1016
1017
			$author = elgg_get_logged_in_user_entity();
1018
			$to_recipients = get_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1019
			$to_recipients_site = get_site_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1020
1021
			$subject = elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'file'), 'en');
1022
			$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'fichier', false), 'fr');
1023
	
1024
			$message = array(
1025
				'cp_topic' => $entity,
1026
				'cp_msg_type' => 'multiple_file',
1027
				'files_information' => $entity,
1028
				'files_uploaded' => $object['files_uploaded'], 
1029
				'cp_msg_type' => 'multiple_file',
1030
				'cp_topic_description_discussion' => 'Please view the files here',
1031
				'cp_topic_description_discussion2' => 'SVP voir les fichiers ici',
1032
			);
1033
1034
			
1035
			$content_entity = $object['files_uploaded'];
1036
			$object = get_entity($object['files_uploaded'][0]);
1037
			$author = elgg_get_logged_in_user_entity();
1038
			break;
1039
1040
		case 'discussion_reply':
1041
		case 'comment':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1042
1043
			// if mentions plugin is enabled... check to see if there were any mentions
1044
			$cp_mentioned_users = (elgg_is_active_plugin('mentions') && $object->getSubtype() !== 'messages') ? cp_scan_mentions($object) : "";
1045
1046
			// send mentioned users a notification
1047
			if (sizeof($cp_mentioned_users) > 0 && is_array($cp_mentioned_users)) {
1048
				for ($i = 0; $i < sizeof($cp_mentioned_users); $i++) {
1049
					$cp_mentioned_user = $cp_mentioned_users[$i];
1050
					$mentioned_user = get_user_by_username(substr($cp_mentioned_user, 1));
1051
1052
					if (!$mentioned_user)
1053
						break;
1054
1055
					$subject = elgg_echo('cp_notify:subject:mention',array($object->getOwnerEntity()->name), 'en') .' | ' . elgg_echo('cp_notify:subject:mention',array($object->getOwnerEntity()->name), 'fr');
1056
					$message = array(
1057
						'cp_msg_type' => 'cp_mention_type',
1058
						'cp_author' => $object->getOwnerEntity()->name,
1059
						'cp_content_desc' => $object->description,
1060
						'cp_link' => $object->getURL(),
1061
						'cp_content' => $object,
1062
					);
1063
					$template = elgg_view('cp_notifications/email_template', $message);
1064
					$user_setting = elgg_get_plugin_user_setting('cpn_mentions_email', $mentioned_user->guid, 'cp_notifications');
1065
1066
					if (strcmp($user_setting, 'mentions_email') == 0) {
1067
						$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $mentioned_user->guid, 'cp_notifications');
1068
1069
						// send digest
1070
						if (strcmp($user_setting, "set_digest_yes") == 0) {
1071
							create_digest($object->getOwnerEntity(), "cp_mention", $object, $mentioned_user);
1072
1073
						// send email and site notification
1074
						} else {
1075
1076
							if (elgg_is_active_plugin('phpmailer'))
1077
								phpmailer_send( $mentioned_user->email, $mentioned_user->name, $subject, $template, NULL, true );
1078
							else
1079
								mail($mentioned_user->email,$subject,$template,cp_get_headers());
1080
						}
1081
					}
1082
1083
					$user_setting = elgg_get_plugin_user_setting('cpn_mentions_site', $mentioned_user->guid, 'cp_notifications');
1084
					$site_template = elgg_view('cp_notifications/site_template', $message);
1085
					if (strcmp($user_setting, 'mentions_site') == 0)
1086
						messages_send($subject, $site_template, $mentioned_user->guid, $site->guid, 0, true, false);
1087
				}
1088
			}
1089
1090
			// retrieve all necessary information for notification
1091
			$container_entity = $object->getContainerEntity();
1092
1093
			$user_comment = get_user($object->owner_guid);
1094
			$topic_container = $container_entity->getContainerEntity();
1095
1096
			// comment or reply in a group
1097
			if ($topic_container instanceof ElggGroup) {
1098
				if (strcmp($object->getSubtype(), 'discussion_reply') == 0) {
1099
					$subject = elgg_echo('cp_notify:subject:comments_discussion',array(gc_explode_translation($topic_container->name,'en')),'en');
1100
					$subject .= ' | '.elgg_echo('cp_notify:subject:comments_discussion',array(gc_explode_translation($topic_container->name,'fr')),'fr');
1101
				} else {
1102
					$subject = elgg_echo('cp_notify:subject:comments',array(gc_explode_translation($topic_container->name,'en')),'en');
1103
					$subject .= ' | '.elgg_echo('cp_notify:subject:comments',array(gc_explode_translation($topic_container->name,'fr')),'fr');
1104
				}
1105
1106
			// comment or reply in a user
1107
			} else {
1108
				$entity_residence = 'usr';
1109
				$subject = elgg_echo('cp_notify:subject:comments_user', array($topic_container->name), 'en');
1110
				$subject .= ' | '.elgg_echo('cp_notify:subject:comments_user', array($topic_container->name), 'fr');
1111
			}
1112
1113
1114
			$message = array(
1115
				'cp_container' => $entity_residence,
1116
				'cp_user_comment' => $user_comment,
1117
				'cp_topic' => $container_entity,
1118
				'cp_topic_type' => cp_translate_subtype($container_entity->getSubtype()),
1119
				'cp_comment' => $object,
1120
				'cp_msg_type' => 'cp_reply_type',
1121
			);
1122
1123
			// digest information purposes
1124
			$content_entity = $container_entity;
1125
			$author = $user_comment;
1126
1127
			// the user creating the content is automatically subscribed to it
1128
			if (elgg_instanceof($container, 'group')) {
1129
	 			if($container->isMember($user_comment)){
1130
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $container_entity->getGUID());
1131
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $container_entity->getGUID());
1132
				}
1133
			} else {
1134
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $container_entity->getGUID());
1135
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $container_entity->getGUID());
1136
			}
1137
1138
1139
			$to_recipients = get_subscribers($dbprefix, $object->getOwnerGUID(), $object->getContainerGUID());
1140
			$to_recipients_site = get_subscribers($dbprefix, $object->getOwnerGUID(), $object->getContainerGUID());
1141
			break;
1142
1143
		// micromissions / opportunities
1144
		case 'mission':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1145
1146
			$job_type = $object->job_type;		// only need to get this once
1147
			$role_type = $object->role_type;
1148
			$job_type_ids = getMissionTypeMetastringid($job_type, $role_type);
1149
			$opt_in_id = elgg_get_metastring_id('gcconnex_profile:opt:yes');
1150
1151
			// get users who want to be notified about new opportunities by site message and have opted in to this type of oppotrunity
1152
			$op_siteusers = get_data("SELECT ps.id, ps.entity_guid as entity_guid FROM {$dbprefix}private_settings ps JOIN {$dbprefix}metadata md ON ps.entity_guid = md.entity_guid WHERE ps.name = 'plugin:user_setting:cp_notifications:cpn_opportunities_site' AND ps.value = 'opportunities_site' AND md.name_id = {$job_type_ids} AND md.value_id = {$opt_in_id}");
1153
1154
			foreach ($op_siteusers as $result) {
1155
				$userid = $result->entity_guid;
1156
				$user_obj = get_user($userid);
1157
				$to_recipients_site[$userid] = $user_obj;
1158
			}
1159
1160
			// get users who want to be notified about new opportunities by email and have opted in to this type of oppotrunity
1161
			$op_emailusers = get_data("SELECT ps.id, ps.entity_guid as entity_guid FROM {$dbprefix}private_settings ps JOIN {$dbprefix}metadata md ON ps.entity_guid = md.entity_guid WHERE ps.name = 'plugin:user_setting:cp_notifications:cpn_opportunities_email' AND ps.value = 'opportunities_email' AND md.name_id = {$job_type_ids} AND md.value_id = {$opt_in_id}");
1162
1163
			foreach ($op_emailusers as $result) {
1164
				$userid = $result->entity_guid;
1165
				$user_obj = get_user($userid);
1166
				$to_recipients[$userid] = $user_obj;
1167
			}
1168
1169
			$message = array(
1170
				'cp_topic' => $object,
1171
				'cp_msg_type' => 'new_mission',
1172
			);
1173
1174
			// digest information purposes
1175
			$content_entity = $object;
1176
			$author = $object->getOwnerEntity();
1177
1178
			$subject = elgg_echo('cp_new_mission:subject',array(),'en') . ' | ' . elgg_echo('cp_new_mission:subject',array(),'fr');
1179
1180
			// the user creating the content is automatically subscribed to it
1181
			add_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_email', $object->getGUID());
1182
			add_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_site_mail', $object->getGUID());
1183
			break; 
1184
1185
		case 'single_file_upload':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1186
1187
		default:
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
1188
1189
			// cyu - there is an issue with regards to auto-saving drafts
1190
			if (strcmp($object->getSubtype(),'blog') == 0) {
1191
				if (strcmp($object->status,'draft') == 0 || strcmp($object->status,'unsaved_draft') == 0) return;
1192
			}
1193
1194
			// the user creating the content is automatically subscribed to it (with exception that is not a widget, forum, etc..)
1195
			$cp_whitelist = array('blog', 'bookmarks', 'poll', 'groupforumtopic', 'image', 'idea', 'page', 'page_top', 'thewire', 'task_top', 'question', 'answer');
1196
			if (in_array($object->getSubtype(),$cp_whitelist)) {
1197
1198
				if ($object->getSubtype() == 'answer') {// subcribed to the question
1199
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $object->getContainerGUID());
1200
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $object->getContainerGUID());
1201
				}
1202
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $object->getGUID());
1203
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $object->getGUID());
1204
            }
1205
1206
			$user = get_user($object->owner_guid);
1207
			$group = $object->getContainerEntity();
1208
1209
			$group_name = gc_explode_translation($group->name,'en');
1210
			$group_name2 = gc_explode_translation($group->name,'fr');
1211
1212
			// fem and mas are different (so the subject should be reflected on which subtype is passed)
1213
			$subtypes_gender_subject = array(
1214
1215
				'blog' => elgg_echo('cp_notify:subject:new_content_mas',array("blogue",$group_name2),'fr'),
1216
				'bookmarks' => elgg_echo('cp_notify:subject:new_content_mas',array("signet",$group_name2),'fr'),
1217
				'file' => elgg_echo('cp_notify:subject:new_content_mas',array("fichier",$group_name2),'fr'),
1218
				'poll' => elgg_echo('cp_notify:subject:new_content_mas',array("sondage",$group_name2),'fr'),
1219
				'event_calendar' => elgg_echo('cp_notify:subject:new_content_mas',array("nouvel événement",$group_name2),'fr'),
1220
				'album' => elgg_echo('cp_notify:subject:new_content_mas',array("album d'image",$group_name2),'fr'),
1221
				'groupforumtopic' => elgg_echo('cp_notify:subject:new_content_fem',array("discussion",$group_name2),'fr'),
1222
				'image' => elgg_echo('cp_notify:subject:new_content_fem',array("image",$group_name2),'fr'),
1223
				'idea' => elgg_echo('cp_notify:subject:new_content_fem',array("idée",$group_name2),'fr'),
1224
				'page' => elgg_echo('cp_notify:subject:new_content_fem',array("page",$group_name2),'fr'),
1225
				'page_top' => elgg_echo('cp_notify:subject:new_content_fem',array("page",$group_name2),'fr'),
1226
				'task_top' => elgg_echo('cp_notify:subject:new_content_fem',array("task",$group_name2),'fr'),
1227
				'task' => elgg_echo('cp_notify:subject:new_content_fem',array("task",$group_name2),'fr'),
1228
        		'question' => elgg_echo('cp_notify:subject:new_content_fem',array("question",$group_name2),'fr'),
1229
        		'etherpad' => elgg_echo('cp_notify:subject:new_content_fem',array(elgg_echo('etherpad:single'),$group_name2),'fr')
1230
			);
1231
1232
			// if there is something missing, we can use a fallback string
1233
			$subj_gender = ($subtypes_gender_subject[$object->getSubtype()] == '') ? elgg_echo('cp_notify:subject:new_content_mas',array($user->name,$object->getSubtype(),$object->title),'fr') : $subtypes_gender_subject[$object->getSubtype()];
1234
1235
			// subscribed to content within a group
1236
			if ($object->getContainerEntity() instanceof ElggGroup) {
1237
				$subject = elgg_echo('cp_notify:subject:new_content',array(cp_translate_subtype($object->getSubtype()),$group_name),'en');
1238
				$subject .= ' | '.$subj_gender;
1239
1240
				$guidone = $object->getContainerGUID();
1241
				$author_id = $object->getOwnerGUID();
1242
				$content_id = $object->getContainerGUID();
1243
1244
			// subscribed to users or friends
1245
			} else {
1246
1247
				if (!$object->title) {
1248
					if (strstr($object->getSubtype(),"poll_choice") !== false)
1249
						return true;
1250
1251
					$subject = elgg_echo('cp_notify_usr:subject:new_content2',array($object->getOwnerEntity()->username,cp_translate_subtype($object->getSubtype())),'en');
1252
					$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2',array($object->getOwnerEntity()->username,cp_translate_subtype($object->getSubtype(), false)),'fr');
1253
				} else {
1254
1255
					if (strcmp($object->getSubtype(), 'hjforumpost') != 0 || strcmp($object->getSubtype(), 'hjforumtopic') != 0) {
1256
						if ($object->getSubtype() == 'answer'){
1257
							$question_guid = $object->getContainerGUID();
1258
							$answer_entity = get_entity($question_guid);
1259
					
1260
							$subject = elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, cp_translate_subtype($object->getSubtype()), gc_explode_translation($answer_entity->title,'en')),'en');
1261
							$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content_f',array($object->getOwnerEntity()->username, cp_translate_subtype($object->getSubtype(), false), gc_explode_translation($answer_entity->title,'fr')),'fr');
1262
						
1263
						} else if ($object->getSubtype() == 'etherpad'){
1264
							$subject = elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, elgg_echo('etherpad:single'), gc_explode_translation($object->title,'en')),'en');
1265
							$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content_f',array($object->getOwnerEntity()->username, elgg_echo('etherpad:single'), gc_explode_translation($object->title,'fr')),'fr');
1266
						} else {
1267
							
1268
							$subject = elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, cp_translate_subtype($object->getSubtype()), gc_explode_translation($object->title,'en')),'en');
1269
							$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, cp_translate_subtype($object->getSubtype(), false), gc_explode_translation($object->title,'fr')),'fr');
1270
						}
1271
					}
1272
				}
1273
1274
				$guidone = $object->getOwnerGUID();
1275
				$author_id = $object->getOwnerGUID();
1276
				
1277
				// Get guid of the question
1278
				if($object->getSubtype = 'answer') {
1279
						$content_id = $object->getContainerGUID();
1280
				}
1281
			}
1282
	
1283
			// client wants the html tags stripped from the notifications
1284
			$object_description = ($object->description != strip_tags($object->description)) ? "" : $object->description;
1285
1286
			$message = array(
1287
				'cp_topic' => $object,
1288
				'cp_msg_type' => 'cp_new_type',
1289
				'cp_topic_description_discussion' => $object->description,
1290
				'cp_topic_description_discussion2' => $object->description2,
1291
			);
1292
1293
			$content_entity = $object;
1294
			$author = $object->getOwnerEntity();
1295
1296
			$to_recipients = get_subscribers($dbprefix, $author_id, $content_id);
1297
			$to_recipients_site = get_site_subscribers($dbprefix, $author_id, $content_id);
1298
			break;
1299
1300
	} // end of switch statement
1301
1302
1303
1304
	$notification_error_type = "";
1305
1306
	// check for empty subjects or empty content
1307
	if (empty($subject)) return false;
1308
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
1309
1310
	/// send the email notification
1311
	if (is_array($to_recipients)) {
1312
1313
		foreach ($to_recipients as $to_recipient) {
1314
1315
			$recipient_user = get_user($to_recipient->guid);
1316
			$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid, 'cp_notifications');
1317
1318
			if ($to_recipient->guid == $author->guid)
1319
				continue;
1320
1321
			if (has_access_to_entity($object, $recipient_user) && $object->access_id != 0) {
1322
1323
				if (strcmp($user_setting, "set_digest_yes") == 0) {
1324
					create_digest($author, $switch_case, $content_entity, get_entity($to_recipient->guid));
1325
1326
				} else {
1327
1328
					$template = elgg_view('cp_notifications/email_template', $message);
1329
1330
					if (elgg_is_active_plugin('phpmailer'))
1331
						phpmailer_send( $to_recipient->email, $to_recipient->name, $subject, $template, NULL, true );
1332
					else
1333
						mail($to_recipient->email,$subject,$template,cp_get_headers());
1334
				}
1335
			}
1336
		}
1337
	}
1338
1339
	/// send site notifications
1340
	if (is_array($to_recipients_site)) {
1341
		
1342
		foreach ($to_recipients_site as $to_recipient) {
1343
			$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid, 'cp_notifications');
1344
			$recipient_user = get_user($to_recipient->guid);
1345
1346
			// check to see if the author is the recipient or recipient has the digest enabled
1347
			if ($to_recipient->guid == $author->guid || strcmp($user_setting, "set_digest_yes") == 0)
1348
				continue;
1349
1350
			if (has_access_to_entity($object, $recipient_user) && $object->access_id != 0) {
1351
1352
				$site_template = elgg_view('cp_notifications/site_template', $message);
1353
				messages_send($subject, $site_template, $to_recipient->guid, $site->guid, 0, true, false);
1354
			}
1355
		}
1356
	}
1357
1358
	// register the error, if either of the arrays are not populated
1359
	if (!is_array($to_recipients) || !is_array($to_recipients_site)) {
1360
		notification_logging('error: in cp_create_notification(), $to_recipients or $to_recipients_site is not array');
1361
	}
1362
1363
}
1364
1365
1366
function notification_logging($error_message) {
1367
	// logging mechanism
1368
	if (elgg_is_active_plugin('wet4')) {
1369
		elgg_load_library('GCconnex_logging');
1370
		$errStack = '';
1371
		$errType = 'custom';
1372
		gc_err_logging($error_message, $errStack, 'Notifications',$errType);
1373
	}
1374
}
1375
1376
/**
1377
 * get all the users that are subscribed to a specified entity (content or user id)
1378
 * and return the array of users
1379
 *
1380
 * @param string 			$dbprefix
1381
 * @param integer 			$user_guid
1382
 * @param optional integer 	$entity_guid
1383
1384
 * @param email vs site_mail
1385
 * @return Array <ElggUser>
1386
1387
 */
1388
1389
function get_subscribers($dbprefix, $user_guid, $entity_guid = '') {
1390
	$subscribed_to = ($entity_guid != '') ? $entity_guid : $user_guid;
1391
1392
	$query = "	SELECT DISTINCT u.guid, u.email, u.username, u.name
1393
				FROM {$dbprefix}entity_relationships r LEFT JOIN {$dbprefix}users_entity u ON r.guid_one = u.guid
1394
				WHERE r.guid_one <> {$user_guid} AND r.relationship = 'cp_subscribed_to_email' AND r.guid_two = {$subscribed_to}";
1395
1396
	return get_data($query);
1397
}
1398
1399
function get_site_subscribers($dbprefix, $user_guid, $entity_guid = '') {
1400
	$subscribed_to = ($entity_guid != '') ? $entity_guid : $user_guid;
1401
1402
	$query = " SELECT DISTINCT u.guid, u.email, u.username, u.name
1403
	FROM {$dbprefix}entity_relationships r LEFT JOIN {$dbprefix}users_entity u ON r.guid_one = u.guid
1404
	WHERE r.guid_one <> {$user_guid} AND r.relationship = 'cp_subscribed_to_site_mail' AND r.guid_two = {$subscribed_to}";
1405
1406
	return get_data($query);
1407
}
1408
1409
1410
/**
1411
 * setup crontab either on a daily or weekly basis
1412
 * get users who are subscribed to digest
1413
 * run crontab, retrieve users, send digest, reset timer (update timestamp)
1414
 *
1415
 * @param string $hook    The name of the plugin hook
1416
 * @param string $type    The type of the plugin hook
1417
 * @param mixed  $value   The current value of the plugin hook
1418
 * @param mixed  $params  Data passed from the trigger
1419
 */
1420
function cp_digest_weekly_cron_handler($hook, $entity_type, $return_value, $params) {
1421
	elgg_load_library('elgg:gc_notification:functions');
1422
	$dbprefix = elgg_get_config('dbprefix');
1423
1424
	echo "<p>Starting up the cron job for the Notifications (cp_notifications plugin)</p>";
1425
1426
	// extract all the users who have the digest
1427
	$query = "SELECT id, entity_guid as guid FROM {$dbprefix}private_settings WHERE name = 'plugin:user_setting:cp_notifications:cpn_set_digest' AND value = 'set_digest_yes'";
1428
	$users = get_data($query);
1429
1430
	foreach ($users as $user) {
1431
1432
		$user = get_entity($user->guid);
1433
		$frequency = elgg_get_plugin_user_setting('cpn_set_digest_frequency', $user->guid, 'cp_notifications');
1434
1435
		if ($user instanceof ElggUser && strcmp($frequency,'set_digest_weekly') == 0 ) {
1436
			$digest_array = array();
1437
1438
			$query = "SELECT * FROM notification_digest WHERE user_guid = {$user->guid}";
1439
			$digest_items = get_data($query);
1440
1441
			$language_preference = (strcmp(elgg_get_plugin_user_setting('cpn_set_digest_language', $user->guid, 'cp_notifications'),'set_digest_en') == 0) ? 'en' : 'fr';
1442
1443
			foreach ($digest_items as $digest_item) {
1444
1445
				// check to make sure that the string is encoded with base 64 or not (legacy)
1446
				if (isJson($digest_item->notification_entry))
1447
					$notification_entry = $digest_item->notification_entry;
1448
				else
1449
					$notification_entry = base64_decode($digest_item->notification_entry);
1450
1451
1452
				if ($digest_item->entry_type === 'group')
1453
					$digest_array[$digest_item->entry_type][base64_decode($digest_item->group_name)][$digest_item->action_type][$digest_item->entity_guid] = $notification_entry;
1454
				else
1455
					$digest_array[$digest_item->entry_type][$digest_item->action_type][$digest_item->entity_guid] = $notification_entry;
1456
1457
			}
1458
1459
			$subject = elgg_echo('cp_newsletter:subject:weekly', $language_preference);
1460
1461
			// if the array is empty, send the empty template
1462
			if (sizeof($digest_array) > 0 || !empty($digest_array))
1463
				$template = elgg_view('cp_notifications/newsletter_template', array('to' => $user, 'newsletter_content' => $digest_array));
1464
			else
1465
				$template = elgg_view('cp_notifications/newsletter_template_empty', array('to' => $user));
1466
1467
			$template = str_replace("<", "\r\n<", $template);
1468
			
1469
			if (elgg_is_active_plugin('phpmailer'))
1470
				phpmailer_send($user->email, $user->name, $subject, $template, NULL, true );
1471
			else
1472
				mail($user->email, $subject, $template, cp_get_headers());
1473
1474
			// delete and clean up the notification, already sent so we don't need to keep it anymore
1475
			$query = "DELETE FROM notification_digest WHERE user_guid = {$user->getGUID()}";
1476
			$result = delete_data($query);
1477
1478
		}
1479
	}
1480
}
1481
1482
1483
1484
/**
1485
 * setup crontab either on a daily or weekly basis
1486
 * get users who are subscribed to digest
1487
 * run crontab, retrieve users, send digest, reset timer (update timestamp)
1488
 *
1489
 * @param string $hook    The name of the plugin hook
1490
 * @param string $entity_type    The type of the plugin hook
1491
 * @param mixed  $return_value   The current value of the plugin hook
1492
 * @param mixed  $params  Data passed from the trigger
1493
 */
1494
function cp_digest_daily_cron_handler($hook, $entity_type, $return_value, $params) {
1495
	elgg_load_library('elgg:gc_notification:functions');
1496
	$dbprefix = elgg_get_config('dbprefix');
1497
1498
	echo "<p>Starting up the cron job for the Notifications (cp_notifications plugin)</p>";
1499
1500
	// extract all the users who have the digest
1501
	$query = "SELECT id, entity_guid as guid FROM {$dbprefix}private_settings WHERE name = 'plugin:user_setting:cp_notifications:cpn_set_digest' AND value = 'set_digest_yes'";
1502
	$users = get_data($query);
1503
1504
	foreach ($users as $user) {
1505
1506
		$user = get_entity($user->guid);
1507
		$frequency = elgg_get_plugin_user_setting('cpn_set_digest_frequency', $user->guid, 'cp_notifications');
1508
1509
		if ($user instanceof ElggUser && strcmp($frequency,'set_digest_daily') == 0 ) {
1510
			$digest_array = array();
1511
1512
			$query = "SELECT * FROM notification_digest WHERE user_guid = {$user->guid}";
1513
			$digest_items = get_data($query);
1514
1515
			$language_preference = (strcmp(elgg_get_plugin_user_setting('cpn_set_digest_language', $user->guid, 'cp_notifications'),'set_digest_en') == 0) ? 'en' : 'fr';
1516
1517
			foreach ($digest_items as $digest_item) {
1518
1519
				// check to make sure that the string is encoded with base 64 or not (legacy)
1520
				if (isJson($digest_item->notification_entry))
1521
					$notification_entry = $digest_item->notification_entry;
1522
				else
1523
					$notification_entry = base64_decode($digest_item->notification_entry);
1524
1525
1526
				if ($digest_item->entry_type === 'group') 
1527
					$digest_array[$digest_item->entry_type][base64_decode($digest_item->group_name)][$digest_item->action_type][$digest_item->entity_guid] = $notification_entry;
1528
				else
1529
					$digest_array[$digest_item->entry_type][$digest_item->action_type][$digest_item->entity_guid] = $notification_entry;
1530
1531
			}
1532
1533
			$subject = elgg_echo('cp_newsletter:subject:daily',$language_preference);
1534
1535
			// if the array is empty, send the empty template
1536
			if (sizeof($digest_array) > 0 || !empty($digest_array))
1537
				$template = elgg_view('cp_notifications/newsletter_template', array('to' => $user, 'newsletter_content' => $digest_array));
1538
			else
1539
				$template = elgg_view('cp_notifications/newsletter_template_empty', array('to' => $user));
1540
1541
1542
			/// e-mail providers can potentially break html codes because it exceeds a limit (set by the inbox)
1543
			/// REFERENCE: https://stackoverflow.com/questions/12216228/html-email-annoying-line-breaking
1544
			$template = str_replace("<", "\r\n<", $template);
1545
1546
			echo $template . "<br/><br/>";
1547
1548
			if (elgg_is_active_plugin('phpmailer'))
1549
				phpmailer_send($user->email, $user->name, $subject, $template, NULL, true );
1550
			else
1551
				mail($user->email, $subject, $template, cp_get_headers());
1552
1553
1554
			// delete and clean up the notification, already sent so we don't need to keep it anymore
1555
			$query = "DELETE FROM notification_digest WHERE user_guid = {$user->getGUID()}";
1556
			$result = delete_data($query);
1557
1558
		}
1559
	}
1560
}
1561
1562
1563
/**
1564
 * check if user has access then prepare the notification and send (if applicable)
1565
 *
1566
 * @param ElggObject					$entity			entity that has been created
1567
 * @param array(guid, email, username)	$to_user		recipient
1568
 * @param array(various)				$message		message that will be in the notification
1569
 * @param int							$guid_two		author or group id (check if subscribe to friend or group)
1570
 */
1571
function cp_notification_preparation_send($entity, $to_user, $message, $guid_two, $subject) {
1572
1573
	$template = elgg_view('cp_notifications/email_template', $message);
1574
1575
	// TODO: fix up mission
1576
	if (strcmp($entity->getSubtype(),'mission') == 0) {
1577
		// send out emails
1578
		if ($to_user->getGUID() != elgg_get_logged_in_user_guid()) { // prevents notification to be sent to the sender
1579
			if ($to_user instanceof ElggUser) {
1580
				if (elgg_is_active_plugin('phpmailer'))
1581
					phpmailer_send( $to_user->email, $to_user->name, $subject, $template, NULL, true );
1582
				else
1583
					mail($to_user->email,$subject,$template,cp_get_headers());
1584
			}
1585
1586
		} else {
1587
			// check if user has access to the content (DO NOT send if user has no access to this object)
1588
1589
			if (has_access_to_entity($entity, $recipient_user) && $object->access_id != 0) {
1590
				//  GCCON-175: assemble the email content with correct username (for notification page)
1591
				$message['user_name'] = $to_user->username;
1592
1593
				// check if user subscribed to receiving notifications
1594
				if (check_entity_relationship($to_user->guid, 'cp_subscribed_to_email', $guid_two))
1595
				{
1596
					if (elgg_is_active_plugin('phpmailer'))
1597
						phpmailer_send( $to_user->email, $to_user->name, $subject, $template, NULL, true );
1598
					else
1599
						mail($to_user->email,$subject,$template,cp_get_headers());
1600
				}
1601
1602
				// send a site notification
1603
				if (check_entity_relationship($to_user->guid, 'cp_subscribed_to_site_mail', $guid_two)) {
1604
					messages_send($subject, $template, $to_recipient->guid, $site->guid, 0, true, false);
1605
				}
1606
			} // end if (check for access)
1607
1608
		}
1609
	}
1610
}
1611
1612
1613
/**
1614
 * cp_send_new_password_request
1615
 *
1616
 * In order to modify core code, this action had to be overwritten.
1617
 * This is a mirror image of the core function: /engine/classes/Elgg/PasswordService.php
1618
 *
1619
 * Only need to send an e-mail notification out, no need to send a site-mail
1620
 *
1621
 * @param ElggUser 	$user 	user entity
1622
 */
1623
function cp_send_new_password_request($user) {
1624
	if (!$user instanceof ElggUser)
1625
		return false;
1626
1627
	// generate code
1628
	$code = generate_random_cleartext_password();
1629
	$user->setPrivateSetting('passwd_conf_code', $code);
1630
	$user->setPrivateSetting('passwd_conf_time', time());
1631
1632
	$link = elgg_get_site_url()."changepassword?u={$user->guid}&c={$code}";
1633
	$ip_address = _elgg_services()->request->getClientIp();
1634
1635
	// we don't need to check if the plugin (cp_notifications) is enabled here
1636
	$message = array(
1637
		'cp_password_request_user' => $user->username,
1638
		'cp_password_request_ip' => $ip_address,
1639
		'cp_password_request_url' => $link,
1640
		'cp_msg_type' => 'cp_forgot_password',
1641
	);
1642
1643
	$subject = elgg_echo('cp_notify:subject:forgot_password', array(), "en");
1644
	$subject .= ' | '.elgg_echo('cp_notify:subject:forgot_password', array(), "fr");
1645
	$template = elgg_view('cp_notifications/email_template', $message);
1646
1647
	if (elgg_is_active_plugin('phpmailer'))
1648
		phpmailer_send( $user->email, $user->name, $subject, $template );
1649
	else
1650
		mail($user->email,$subject,$template,cp_get_headers());
1651
}
1652
1653
1654
/*
1655
 * cp_membership_request
1656
 *
1657
 * replaced the event (see init()) so we can send out notifications through this plugin instead
1658
 *
1659
 */
1660
function cp_membership_request($event, $type, $object) { 	// MUST always be sending notification
1661
	$request_user = get_user($object->guid_one); 			// user who sends request to join
1662
	$group_request = get_entity($object->guid_two);			// group that is being requested
1663
1664
	$message = array(
1665
		'cp_group_req_user' => $request_user,
1666
		'cp_group_req_group' => $group_request,
1667
		'cp_msg_type' => 'cp_closed_grp_req_type',
1668
	);
1669
	$template = elgg_view('cp_notifications/email_template', $message);
1670
	$subject = elgg_echo('cp_notify:subject:group_request',array($request_user->name, gc_explode_translation($group_request->name,'en')),'en');
1671
	$subject .= ' | '.elgg_echo('cp_notify:subject:group_request',array($request_user->name, gc_explode_translation($group_request->name,'fr')),'fr');
1672
1673
	$to_user = get_user($group_request->owner_guid);
1674
	if (elgg_is_active_plugin('phpmailer')) {
1675
		phpmailer_send( $to_user->email, $to_user->name, $subject, $template, NULL, true );
1676
	} else {
1677
		mail($to_user->email,$subject,$template,cp_get_headers());
1678
	}
1679
	messages_send($subject, $template, $to_user->guid, elgg_get_site_entity()->guid, 0, true, false);
1680
}
1681
1682
1683
1684
1685
/**
1686
 * intercepts all email and stops emails from sending
1687
 *
1688
 * @param string $hook    The name of the plugin hook
1689
 * @param string $type    The type of the plugin hook
1690
 * @param mixed  $value   The current value of the plugin hook
1691
 * @param mixed  $params  Data passed from the trigger
1692
 */
1693
function cpn_email_handler_hook($hook, $type, $notification, $params) {
1694
	return false;
1695
}
1696
1697
1698
/**
1699
 * implements the icons (likes, in this case) within the context of the entity
1700
 * TODO: make the likes button act as AJAX
1701
 *
1702
 * @param string $hook    The name of the plugin hook
1703
 * @param string $type    The type of the plugin hook
1704
 * @param mixed  $value   The current value of the plugin hook
1705
 * @param mixed  $params  Data passed from the trigger
1706
 *
1707
 * @return mixed if not null, this will be the new value of the plugin hook
1708
 */
1709
function notify_entity_menu_setup($hook, $type, $return, $params) {
1710
	$entity = $params['entity'];
1711
	$do_not_subscribe_list = array('comment','discussion_reply','widget');
1712
	if (elgg_in_context('widgets') || in_array($entity->getSubtype(), $do_not_subscribe_list))  return $return;
1713
1714
	// cyu - check for everything to put the bell thingy (xor)
1715
	$allow_subscription = false;
1716
	if ( $entity->getContainerEntity() instanceof ElggGroup ) {
1717
		$allow_subscription = ($entity->getContainerEntity()->isMember(elgg_get_logged_in_user_entity()) == 1 ) ? true : false;
1718
1719
	} else if ($entity instanceof ElggGroup) {
1720
		$allow_subscription =  ($entity->isMember(elgg_get_logged_in_user_entity()) == 1 ) ? true : false;
1721
1722
	} else if ( $entity->getContainerEntity() instanceof ElggUser )
1723
		$allow_subscription = true;
1724
1725
	if ($entity instanceof ElggGroup) {
1726
		$entType = 'group';
1727
		if($entity->title3){
1728
				$entName = gc_explode_translation($entity->title3, get_current_language());
1729
		}else{
1730
				$entName = $entity->name;
1731
		}
1732
	} else {
1733
		if(!in_array($entity->getSubtype(), array('comment', 'discussion_reply', 'thewire'))){
1734
			if($entity->title3){
1735
					$entName = gc_explode_translation($entity->title3, get_current_language());
1736
			}else{
1737
					$entName = $entity->title;
1738
			}
1739
		} else {
1740
			$entName = $entity->getOwnerEntity()->name;
1741
		}
1742
		$entType = $entity->getSubtype();
1743
	}
1744
1745
	if ($allow_subscription && elgg_is_logged_in()) {
1746
	    if ( check_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_email', $entity->getGUID()) || check_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_site_mail', $entity->getGUID()) ) {
1747
1748
1749
			$bell_status = (elgg_is_active_plugin('wet4')) ? '<i class="icon-unsel fa fa-lg fa-bell"><span class="wb-inv">'.elgg_echo('entity:unsubscribe:link:'.$entType, array($entName)).'</span></i>' : elgg_echo('cp_notify:stop_subscribe');
1750
1751
1752
		    $return[] = ElggMenuItem::factory(array(
1753
			    'name' => 'unset_notify',
1754
			    'href' => elgg_add_action_tokens_to_url("/action/cp_notify/unsubscribe?guid={$entity->guid}"),
1755
			    'text' => $bell_status,
1756
			    'title' => elgg_echo('cp_notify:unsubBell'),
1757
			    'priority' => 1000,
1758
			    'class' => 'bell-subbed',
1759
			    'item_class' => ''
1760
		    ));
1761
1762
	    } else {
1763
1764
		    $bell_status = (elgg_is_active_plugin('wet4')) ? '<i class="icon-unsel fa fa-lg fa-bell-slash-o"><span class="wb-inv">'.elgg_echo('entity:subscribe:link:'.$entType, array($entName)).'</span></i>' : elgg_echo('cp_notify:start_subscribe');
1765
1766
1767
		    $return[] = ElggMenuItem::factory(array(
1768
			    'name' => 'set_notify',
1769
			    'href' => elgg_add_action_tokens_to_url("/action/cp_notify/subscribe?guid={$entity->guid}"),
1770
			    'text' => $bell_status,
1771
			    'title' => elgg_echo('cp_notify:subBell'),
1772
			    'priority' => 1000,
1773
			    'class' => '',
1774
			    'item_class' => ''
1775
		    ));
1776
		}
1777
	}
1778
	return $return;
1779
}
1780
1781