3 * @file mod/notifications.php
4 * @brief The notifications module
8 use Friendica\Content\ContactSelector;
9 use Friendica\Content\Nav;
10 use Friendica\Core\L10n;
11 use Friendica\Core\NotificationsManager;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15 use Friendica\Module\Login;
17 function notifications_post(App $a)
20 goaway(System::baseUrl());
23 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
25 if ($request_id === 'all') {
30 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
32 if (DBA::isResult($intro)) {
33 $intro_id = $intro['id'];
34 $contact_id = $intro['contact-id'];
36 notice(L10n::t('Invalid request identifier.') . EOL);
40 // If it is a friend suggestion, the contact is not a new friend but an existing friend
41 // that should not be deleted.
45 if ($_POST['submit'] == L10n::t('Discard')) {
46 DBA::delete('intro', ['id' => $intro_id]);
49 // The check for blocked and pending is in case the friendship was already approved
50 // and we just want to get rid of the now pointless notification
51 $condition = ['id' => $contact_id, 'uid' => local_user(),
52 'self' => false, 'blocked' => true, 'pending' => true];
53 DBA::delete('contact', $condition);
55 goaway('notifications/intros');
58 if ($_POST['submit'] == L10n::t('Ignore')) {
59 DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
60 goaway('notifications/intros');
65 function notifications_content(App $a)
68 notice(L10n::t('Permission denied.') . EOL);
72 $page = defaults($_REQUEST, 'page', 1);
73 $show = defaults($_REQUEST, 'show', 0);
75 Nav::setSelected('notifications');
77 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
79 $nm = new NotificationsManager();
82 // Get the nav tabs for the notification pages
83 $tabs = $nm->getTabs();
85 $notif_nocontent = '';
87 // Notification results per page
89 $startrec = ($page * $perpage) - $perpage;
91 $notif_header = L10n::t('Notifications');
94 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
95 Nav::setSelected('introductions');
97 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
99 $notifs = $nm->introNotifs($all, $startrec, $perpage);
101 // Get the network notifications
102 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
103 $notif_header = L10n::t('Network Notifications');
104 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
106 // Get the system notifications
107 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
108 $notif_header = L10n::t('System Notifications');
109 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
111 // Get the personal notifications
112 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
113 $notif_header = L10n::t('Personal Notifications');
114 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
116 // Get the home notifications
117 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
118 $notif_header = L10n::t('Home Notifications');
119 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
123 $a->set_pager_itemspage($perpage);
125 // Add additional informations (needed for json output)
126 $notifs['items_page'] = $a->pager['itemspage'];
127 $notifs['page'] = $a->pager['page'];
130 if (intval($json) === 1) {
131 System::jsonExit($notifs);
134 $notif_tpl = get_markup_template('notifications.tpl');
137 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
138 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
141 // Process the data for template creation
142 if (defaults($notifs, 'ident', '') === 'introductions') {
143 $sugg = get_markup_template('suggestions.tpl');
144 $tpl = get_markup_template('intros.tpl');
146 // The link to switch between ignored and normal connection requests
148 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
149 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
152 // Loop through all introduction notifications.This creates an array with the output html for each
154 foreach ($notifs['notifications'] as $notif) {
156 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
157 // We have to distinguish between these two because they use different data.
158 switch ($notif['label']) {
159 case 'friend_suggestion':
160 $notif_content[] = replace_macros($sugg, [
161 '$type' => $notif['label'],
162 '$str_notifytype' => L10n::t('Notification type:'),
163 '$notify_type'=> $notif['notify_type'],
164 '$intro_id' => $notif['intro_id'],
165 '$lbl_madeby' => L10n::t('Suggested by:'),
166 '$madeby' => $notif['madeby'],
167 '$madeby_url' => $notif['madeby_url'],
168 '$madeby_zrl' => $notif['madeby_zrl'],
169 '$madeby_addr'=> $notif['madeby_addr'],
170 '$contact_id' => $notif['contact_id'],
171 '$photo' => $notif['photo'],
172 '$fullname' => $notif['name'],
173 '$url' => $notif['url'],
174 '$zrl' => $notif['zrl'],
175 '$lbl_url' => L10n::t('Profile URL'),
176 '$addr' => $notif['addr'],
177 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
178 '$knowyou' => $notif['knowyou'],
179 '$approve' => L10n::t('Approve'),
180 '$note' => $notif['note'],
181 '$request' => $notif['request'],
182 '$ignore' => L10n::t('Ignore'),
183 '$discard' => L10n::t('Discard'),
187 // Normal connection requests
189 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
190 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
198 if ($notif['network'] === Protocol::DFRN) {
199 $lbl_knowyou = L10n::t('Claims to be known to you: ');
200 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
201 $helptext = L10n::t('Shall your connection be bidirectional or not?');
202 $helptext2 = L10n::t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notif['name'], $notif['name']);
203 $helptext3 = L10n::t('Accepting %s as a subscriber allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notif['name']);
204 } elseif ($notif['network'] === Protocol::DIASPORA) {
205 $helptext = L10n::t('Shall your connection be bidirectional or not?');
206 $helptext2 = L10n::t('Accepting %s as a friend allows %s to subscribe to your posts, and you will also receive updates from them in your news feed.', $notif['name'], $notif['name']);
207 $helptext3 = L10n::t('Accepting %s as a sharer allows them to subscribe to your posts, but you will not receive updates from them in your news feed.', $notif['name']);
210 $dfrn_tpl = get_markup_template('netfriend.tpl');
211 $dfrn_text = replace_macros($dfrn_tpl, [
212 '$intro_id' => $notif['intro_id'],
213 '$friend_selected' => $friend_selected,
214 '$fan_selected'=> $fan_selected,
215 '$approve_as1' => $helptext,
216 '$approve_as2' => $helptext2,
217 '$approve_as3' => $helptext3,
218 '$as_friend' => L10n::t('Friend'),
219 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
222 $header = $notif['name'];
224 if ($notif['addr'] != '') {
225 $header .= ' <' . $notif['addr'] . '>';
228 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
230 if ($notif['network'] != Protocol::DIASPORA) {
231 $discard = L10n::t('Discard');
236 $notif_content[] = replace_macros($tpl, [
237 '$type' => $notif['label'],
238 '$header' => htmlentities($header),
239 '$str_notifytype' => L10n::t('Notification type:'),
240 '$notify_type' => $notif['notify_type'],
241 '$dfrn_text' => $dfrn_text,
242 '$dfrn_id' => $notif['dfrn_id'],
243 '$uid' => $notif['uid'],
244 '$intro_id' => $notif['intro_id'],
245 '$contact_id' => $notif['contact_id'],
246 '$photo' => $notif['photo'],
247 '$fullname' => $notif['name'],
248 '$location' => $notif['location'],
249 '$lbl_location'=> L10n::t('Location:'),
250 '$about' => $notif['about'],
251 '$lbl_about' => L10n::t('About:'),
252 '$keywords' => $notif['keywords'],
253 '$lbl_keywords'=> L10n::t('Tags:'),
254 '$gender' => $notif['gender'],
255 '$lbl_gender' => L10n::t('Gender:'),
256 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
257 '$url' => $notif['url'],
258 '$zrl' => $notif['zrl'],
259 '$lbl_url' => L10n::t('Profile URL'),
260 '$addr' => $notif['addr'],
261 '$lbl_knowyou' => $lbl_knowyou,
262 '$lbl_network' => L10n::t('Network:'),
263 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
264 '$knowyou' => $knowyou,
265 '$approve' => L10n::t('Approve'),
266 '$note' => $notif['note'],
267 '$ignore' => L10n::t('Ignore'),
268 '$discard' => $discard,
274 if (count($notifs['notifications']) == 0) {
275 info(L10n::t('No introductions.') . EOL);
278 // Normal notifications (no introductions)
279 } elseif (!empty($notifs['notifications'])) {
280 // Loop trough ever notification This creates an array with the output html for each
281 // notification and apply the correct template according to the notificationtype (label).
282 foreach ($notifs['notifications'] as $notif) {
283 $notification_templates = [
284 'like' => 'notifications_likes_item.tpl',
285 'dislike' => 'notifications_dislikes_item.tpl',
286 'attend' => 'notifications_attend_item.tpl',
287 'attendno' => 'notifications_attend_item.tpl',
288 'attendmaybe' => 'notifications_attend_item.tpl',
289 'friend' => 'notifications_friends_item.tpl',
290 'comment' => 'notifications_comments_item.tpl',
291 'post' => 'notifications_posts_item.tpl',
292 'notify' => 'notify.tpl',
295 $tpl_notif = get_markup_template($notification_templates[$notif['label']]);
297 $notif_content[] = replace_macros($tpl_notif, [
298 '$item_label' => $notif['label'],
299 '$item_link' => $notif['link'],
300 '$item_image' => $notif['image'],
301 '$item_url' => $notif['url'],
302 '$item_text' => $notif['text'],
303 '$item_when' => $notif['when'],
304 '$item_ago' => $notif['ago'],
305 '$item_seen' => $notif['seen'],
309 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
312 $o .= replace_macros($notif_tpl, [
313 '$notif_header' => $notif_header,
315 '$notif_content' => $notif_content,
316 '$notif_nocontent' => $notif_nocontent,
317 '$notif_show_lnk' => $notif_show_lnk,
318 '$notif_paginate' => alt_pager($a, count($notif_content))