Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/site_notifications/start.php (4 issues)

1
<?php
2
/**
3
 * Site notifications
4
 *
5
 * @todo check for notifications when setting topbar icon
6
 * @todo add a remove visible and all notifications button
7
 */
8
9
/**
10
 * Site notifications init
11
 *
12
 * @return void
13
 */
14
function site_notifications_init() {
15
	// register as a notification type
16 31
	elgg_register_notification_method('site');
17 31
	elgg_register_plugin_hook_handler('send', 'notification:site', 'site_notifications_send');
18
	
19 31
	elgg_register_plugin_hook_handler('register', 'menu:entity', 'site_notifications_register_entity_menu');
20
21 31
	elgg_register_page_handler('site_notifications', 'site_notifications_page_handler');
0 ignored issues
show
Deprecated Code introduced by
The function elgg_register_page_handler() has been deprecated: 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

21
	/** @scrutinizer ignore-deprecated */ elgg_register_page_handler('site_notifications', 'site_notifications_page_handler');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
22
23 31
	elgg_extend_view('elgg.css', 'site_notifications/css');
24
25 31
	$js = elgg_get_simplecache_url('site_notifications.js');
26 31
	elgg_register_js('elgg.site_notifications', $js, 'footer');
27
28 31
	site_notifications_set_topbar();
29 31
}
30
31
/**
32
 * Page handler
33
 *
34
 * /site_notifications/view/<username>
35
 *
36
 * @param array $segments URL segments
37
 *
38
 * @return bool
39
 */
40
function site_notifications_page_handler($segments) {
41
	elgg_gatekeeper();
42
43
	if (!isset($segments[1])) {
44
		$segments[1] = elgg_get_logged_in_user_entity()->username;
45
	}
46
47
	$user = get_user_by_username($segments[1]);
48
	if (!$user) {
49
		return false;
50
	}
51
52
	elgg_set_page_owner_guid($user->guid);
53
54
	echo elgg_view_resource('site_notifications/view');
55
56
	return true;
57
}
58
59
/**
60
 * Sets the topbar notify icon and text
61
 *
62
 * @return void
63
 */
64
function site_notifications_set_topbar() {
65
	
66 31
	if (!elgg_is_logged_in()) {
67 31
		return;
68
	}
69
	
70
	elgg_register_menu_item('topbar', [
71
		'name' => 'site_notifications',
72
		'href' => 'site_notifications/view/' . elgg_get_logged_in_user_entity()->username,
73
		'text' => elgg_echo('site_notifications:topbar'),
74
		'icon' => 'bell',
75
		'priority' => 100,
76
	]);
77
}
78
79
/**
80
 * Create a site notification
81
 *
82
 * @param string $hook   Hook name
83
 * @param string $type   Hook type
84
 * @param bool   $result Has the notification been sent
85
 * @param array  $params Hook parameters
86
 *
87
 * @return void|true
88
 */
89
function site_notifications_send($hook, $type, $result, $params) {
3 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

89
function site_notifications_send($hook, /** @scrutinizer ignore-unused */ $type, $result, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

89
function site_notifications_send(/** @scrutinizer ignore-unused */ $hook, $type, $result, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

89
function site_notifications_send($hook, $type, /** @scrutinizer ignore-unused */ $result, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
	/* @var Elgg\Notifications\Notification */
91 2
	$notification = elgg_extract('notification', $params);
92 2
	if ($notification->summary) {
93
		$message = $notification->summary;
94
	} else {
95 2
		$message = $notification->subject;
96
	}
97
98 2
	if (isset($params['event'])) {
99 2
		$event = $params['event'];
100 2
		$object = $event->getObject();
101
	} else {
102
		$object = null;
103
	}
104
105 2
	$actor = $notification->getSender();
106 2
	$recipient = $notification->getRecipient();
107 2
	$url = $notification->url;
108
	
109 2
	$ia = elgg_set_ignore_access(true);
110 2
	$note = SiteNotificationFactory::create($recipient, $message, $actor, $object, $url);
111 2
	elgg_set_ignore_access($ia);
112 2
	if ($note) {
113 2
		return true;
114
	}
115
}
116
117
/**
118
 * Fixes unwanted menu items on the entity menu
119
 *
120
 * @param \Elgg\Hook $hook Hook
121
 *
122
 * @return void|\ElggMenuItem[]
123
 */
124
function site_notifications_register_entity_menu(\Elgg\Hook $hook) {
125 1
	$entity = $hook->getEntityParam();
126 1
	if (!($entity instanceof SiteNotification)) {
127 1
		return;
128
	}
129
	
130
	$return = $hook->getValue();
131
	foreach ($return as $index => $menu_item) {
132
		if ($menu_item->getName() === 'edit') {
133
			unset($return[$index]);
134
			continue;
135
		}
136
		
137
		if ($menu_item->getName() === 'delete') {
138
			$menu_item->setLinkClass('site-notifications-delete');
139
			$menu_item->{"data-entity-ref"} = 'elgg-object-' . $entity->guid;
140
		}
141
	}
142
	
143
	return $return;
144
}
145
146
return function() {
147 18
	elgg_register_event_handler('init', 'system', 'site_notifications_init');
148
};
149