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;
16 function notifications_post(App $a)
19 goaway(System::baseUrl());
22 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
24 if ($request_id === 'all') {
29 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
31 if (DBA::isResult($intro)) {
32 $intro_id = $intro['id'];
33 $contact_id = $intro['contact-id'];
35 notice(L10n::t('Invalid request identifier.') . EOL);
39 // If it is a friend suggestion, the contact is not a new friend but an existing friend
40 // that should not be deleted.
44 if ($_POST['submit'] == L10n::t('Discard')) {
45 DBA::delete('intro', ['id' => $intro_id]);
48 // The check for blocked and pending is in case the friendship was already approved
49 // and we just want to get rid of the now pointless notification
50 $condition = ['id' => $contact_id, 'uid' => local_user(),
51 'self' => false, 'blocked' => true, 'pending' => true];
52 DBA::delete('contact', $condition);
54 goaway('notifications/intros');
57 if ($_POST['submit'] == L10n::t('Ignore')) {
58 DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
59 goaway('notifications/intros');
64 function notifications_content(App $a)
67 notice(L10n::t('Permission denied.') . EOL);
71 $page = defaults($_REQUEST, 'page', 1);
72 $show = defaults($_REQUEST, 'show', 0);
74 Nav::setSelected('notifications');
76 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
78 $nm = new NotificationsManager();
81 // Get the nav tabs for the notification pages
82 $tabs = $nm->getTabs();
84 $notif_nocontent = '';
86 // Notification results per page
88 $startrec = ($page * $perpage) - $perpage;
90 $notif_header = L10n::t('Notifications');
93 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
94 Nav::setSelected('introductions');
96 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
98 $notifs = $nm->introNotifs($all, $startrec, $perpage);
100 // Get the network notifications
101 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
102 $notif_header = L10n::t('Network Notifications');
103 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
105 // Get the system notifications
106 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
107 $notif_header = L10n::t('System Notifications');
108 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
110 // Get the personal notifications
111 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
112 $notif_header = L10n::t('Personal Notifications');
113 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
115 // Get the home notifications
116 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
117 $notif_header = L10n::t('Home Notifications');
118 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
122 $a->set_pager_itemspage($perpage);
124 // Add additional informations (needed for json output)
125 $notifs['items_page'] = $a->pager['itemspage'];
126 $notifs['page'] = $a->pager['page'];
129 if (intval($json) === 1) {
130 System::jsonExit($notifs);
133 $notif_tpl = get_markup_template('notifications.tpl');
136 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
137 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
140 // Process the data for template creation
141 if (defaults($notifs, 'ident', '') === 'introductions') {
142 $sugg = get_markup_template('suggestions.tpl');
143 $tpl = get_markup_template('intros.tpl');
145 // The link to switch between ignored and normal connection requests
147 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
148 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
151 // Loop through all introduction notifications.This creates an array with the output html for each
153 foreach ($notifs['notifications'] as $notif) {
155 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
156 // We have to distinguish between these two because they use different data.
157 switch ($notif['label']) {
158 case 'friend_suggestion':
159 $notif_content[] = replace_macros($sugg, [
160 '$type' => $notif['label'],
161 '$str_notifytype' => L10n::t('Notification type:'),
162 '$notify_type'=> $notif['notify_type'],
163 '$intro_id' => $notif['intro_id'],
164 '$lbl_madeby' => L10n::t('Suggested by:'),
165 '$madeby' => $notif['madeby'],
166 '$madeby_url' => $notif['madeby_url'],
167 '$madeby_zrl' => $notif['madeby_zrl'],
168 '$madeby_addr'=> $notif['madeby_addr'],
169 '$contact_id' => $notif['contact_id'],
170 '$photo' => $notif['photo'],
171 '$fullname' => $notif['name'],
172 '$url' => $notif['url'],
173 '$zrl' => $notif['zrl'],
174 '$lbl_url' => L10n::t('Profile URL'),
175 '$addr' => $notif['addr'],
176 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
177 '$knowyou' => $notif['knowyou'],
178 '$approve' => L10n::t('Approve'),
179 '$note' => $notif['note'],
180 '$request' => $notif['request'],
181 '$ignore' => L10n::t('Ignore'),
182 '$discard' => L10n::t('Discard'),
186 // Normal connection requests
188 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
189 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
197 if ($notif['network'] === Protocol::DFRN) {
198 $lbl_knowyou = L10n::t('Claims to be known to you: ');
199 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
200 $helptext = L10n::t('Shall your connection be bidirectional or not?');
201 $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']);
202 $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']);
203 } elseif ($notif['network'] === Protocol::DIASPORA) {
204 $helptext = L10n::t('Shall your connection be bidirectional or not?');
205 $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']);
206 $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']);
209 $dfrn_tpl = get_markup_template('netfriend.tpl');
210 $dfrn_text = replace_macros($dfrn_tpl, [
211 '$intro_id' => $notif['intro_id'],
212 '$friend_selected' => $friend_selected,
213 '$fan_selected'=> $fan_selected,
214 '$approve_as1' => $helptext,
215 '$approve_as2' => $helptext2,
216 '$approve_as3' => $helptext3,
217 '$as_friend' => L10n::t('Friend'),
218 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
221 $header = $notif['name'];
223 if ($notif['addr'] != '') {
224 $header .= ' <' . $notif['addr'] . '>';
227 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
229 if ($notif['network'] != Protocol::DIASPORA) {
230 $discard = L10n::t('Discard');
235 $notif_content[] = replace_macros($tpl, [
236 '$type' => $notif['label'],
237 '$header' => htmlentities($header),
238 '$str_notifytype' => L10n::t('Notification type:'),
239 '$notify_type' => $notif['notify_type'],
240 '$dfrn_text' => $dfrn_text,
241 '$dfrn_id' => $notif['dfrn_id'],
242 '$uid' => $notif['uid'],
243 '$intro_id' => $notif['intro_id'],
244 '$contact_id' => $notif['contact_id'],
245 '$photo' => $notif['photo'],
246 '$fullname' => $notif['name'],
247 '$location' => $notif['location'],
248 '$lbl_location'=> L10n::t('Location:'),
249 '$about' => $notif['about'],
250 '$lbl_about' => L10n::t('About:'),
251 '$keywords' => $notif['keywords'],
252 '$lbl_keywords'=> L10n::t('Tags:'),
253 '$gender' => $notif['gender'],
254 '$lbl_gender' => L10n::t('Gender:'),
255 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
256 '$url' => $notif['url'],
257 '$zrl' => $notif['zrl'],
258 '$lbl_url' => L10n::t('Profile URL'),
259 '$addr' => $notif['addr'],
260 '$lbl_knowyou' => $lbl_knowyou,
261 '$lbl_network' => L10n::t('Network:'),
262 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
263 '$knowyou' => $knowyou,
264 '$approve' => L10n::t('Approve'),
265 '$note' => $notif['note'],
266 '$ignore' => L10n::t('Ignore'),
267 '$discard' => $discard,
273 if (count($notifs['notifications']) == 0) {
274 info(L10n::t('No introductions.') . EOL);
277 // Normal notifications (no introductions)
278 } elseif (!empty($notifs['notifications'])) {
279 // Loop trough ever notification This creates an array with the output html for each
280 // notification and apply the correct template according to the notificationtype (label).
281 foreach ($notifs['notifications'] as $notif) {
282 $notification_templates = [
283 'like' => 'notifications_likes_item.tpl',
284 'dislike' => 'notifications_dislikes_item.tpl',
285 'attend' => 'notifications_attend_item.tpl',
286 'attendno' => 'notifications_attend_item.tpl',
287 'attendmaybe' => 'notifications_attend_item.tpl',
288 'friend' => 'notifications_friends_item.tpl',
289 'comment' => 'notifications_comments_item.tpl',
290 'post' => 'notifications_posts_item.tpl',
291 'notify' => 'notify.tpl',
294 $tpl_notif = get_markup_template($notification_templates[$notif['label']]);
296 $notif_content[] = replace_macros($tpl_notif, [
297 '$item_label' => $notif['label'],
298 '$item_link' => $notif['link'],
299 '$item_image' => $notif['image'],
300 '$item_url' => $notif['url'],
301 '$item_text' => $notif['text'],
302 '$item_when' => $notif['when'],
303 '$item_ago' => $notif['ago'],
304 '$item_seen' => $notif['seen'],
308 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
311 $o .= replace_macros($notif_tpl, [
312 '$notif_header' => $notif_header,
314 '$notif_content' => $notif_content,
315 '$notif_nocontent' => $notif_nocontent,
316 '$notif_show_lnk' => $notif_show_lnk,
317 '$notif_paginate' => alt_pager($a, count($notif_content))