3 * @file mod/notifications.php
4 * @brief The notifications module
8 use Friendica\Content\ContactSelector;
9 use Friendica\Content\Nav;
10 use Friendica\Content\Pager;
11 use Friendica\Core\L10n;
12 use Friendica\Core\NotificationsManager;
13 use Friendica\Core\Protocol;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\Module\Login;
18 function notifications_post(App $a)
21 $a->internalRedirect();
24 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
26 if ($request_id === 'all') {
31 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
33 if (DBA::isResult($intro)) {
34 $intro_id = $intro['id'];
35 $contact_id = $intro['contact-id'];
37 notice(L10n::t('Invalid request identifier.') . EOL);
41 // If it is a friend suggestion, the contact is not a new friend but an existing friend
42 // that should not be deleted.
46 if ($_POST['submit'] == L10n::t('Discard')) {
47 DBA::delete('intro', ['id' => $intro_id]);
50 // The check for blocked and pending is in case the friendship was already approved
51 // and we just want to get rid of the now pointless notification
52 $condition = ['id' => $contact_id, 'uid' => local_user(),
53 'self' => false, 'blocked' => true, 'pending' => true];
54 DBA::delete('contact', $condition);
56 $a->internalRedirect('notifications/intros');
59 if ($_POST['submit'] == L10n::t('Ignore')) {
60 DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
61 $a->internalRedirect('notifications/intros');
66 function notifications_content(App $a)
69 notice(L10n::t('Permission denied.') . EOL);
73 $page = defaults($_REQUEST, 'page', 1);
74 $show = defaults($_REQUEST, 'show', 0);
76 Nav::setSelected('notifications');
78 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
80 $nm = new NotificationsManager();
83 // Get the nav tabs for the notification pages
84 $tabs = $nm->getTabs();
86 $notif_nocontent = '';
88 // Notification results per page
90 $startrec = ($page * $perpage) - $perpage;
92 $notif_header = L10n::t('Notifications');
95 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
96 Nav::setSelected('introductions');
98 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
100 $notifs = $nm->introNotifs($all, $startrec, $perpage);
102 // Get the network notifications
103 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
104 $notif_header = L10n::t('Network Notifications');
105 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
107 // Get the system notifications
108 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
109 $notif_header = L10n::t('System Notifications');
110 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
112 // Get the personal notifications
113 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
114 $notif_header = L10n::t('Personal Notifications');
115 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
117 // Get the home notifications
118 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
119 $notif_header = L10n::t('Home Notifications');
120 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
124 $pager = new Pager($a->query_string, $perpage);
126 // Add additional informations (needed for json output)
127 $notifs['items_page'] = $pager->getItemsPerPage();
128 $notifs['page'] = $pager->getPage();
131 if (intval($json) === 1) {
132 System::jsonExit($notifs);
135 $notif_tpl = get_markup_template('notifications.tpl');
138 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
139 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
142 // Process the data for template creation
143 if (defaults($notifs, 'ident', '') === 'introductions') {
144 $sugg = get_markup_template('suggestions.tpl');
145 $tpl = get_markup_template('intros.tpl');
147 // The link to switch between ignored and normal connection requests
149 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
150 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
153 // Loop through all introduction notifications.This creates an array with the output html for each
155 foreach ($notifs['notifications'] as $notif) {
157 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
158 // We have to distinguish between these two because they use different data.
159 switch ($notif['label']) {
160 case 'friend_suggestion':
161 $notif_content[] = replace_macros($sugg, [
162 '$type' => $notif['label'],
163 '$str_notifytype' => L10n::t('Notification type:'),
164 '$notify_type'=> $notif['notify_type'],
165 '$intro_id' => $notif['intro_id'],
166 '$lbl_madeby' => L10n::t('Suggested by:'),
167 '$madeby' => $notif['madeby'],
168 '$madeby_url' => $notif['madeby_url'],
169 '$madeby_zrl' => $notif['madeby_zrl'],
170 '$madeby_addr'=> $notif['madeby_addr'],
171 '$contact_id' => $notif['contact_id'],
172 '$photo' => $notif['photo'],
173 '$fullname' => $notif['name'],
174 '$url' => $notif['url'],
175 '$zrl' => $notif['zrl'],
176 '$lbl_url' => L10n::t('Profile URL'),
177 '$addr' => $notif['addr'],
178 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
179 '$knowyou' => $notif['knowyou'],
180 '$approve' => L10n::t('Approve'),
181 '$note' => $notif['note'],
182 '$request' => $notif['request'],
183 '$ignore' => L10n::t('Ignore'),
184 '$discard' => L10n::t('Discard'),
188 // Normal connection requests
190 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
191 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
199 if ($notif['network'] === Protocol::DFRN) {
200 $lbl_knowyou = L10n::t('Claims to be known to you: ');
201 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
202 $helptext = L10n::t('Shall your connection be bidirectional or not?');
203 $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']);
204 $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']);
205 } elseif ($notif['network'] === Protocol::DIASPORA) {
206 $helptext = L10n::t('Shall your connection be bidirectional or not?');
207 $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']);
208 $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']);
211 $dfrn_tpl = get_markup_template('netfriend.tpl');
212 $dfrn_text = replace_macros($dfrn_tpl, [
213 '$intro_id' => $notif['intro_id'],
214 '$friend_selected' => $friend_selected,
215 '$fan_selected'=> $fan_selected,
216 '$approve_as1' => $helptext,
217 '$approve_as2' => $helptext2,
218 '$approve_as3' => $helptext3,
219 '$as_friend' => L10n::t('Friend'),
220 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
223 $header = $notif['name'];
225 if ($notif['addr'] != '') {
226 $header .= ' <' . $notif['addr'] . '>';
229 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
231 if ($notif['network'] != Protocol::DIASPORA) {
232 $discard = L10n::t('Discard');
237 $notif_content[] = replace_macros($tpl, [
238 '$type' => $notif['label'],
239 '$header' => htmlentities($header),
240 '$str_notifytype' => L10n::t('Notification type:'),
241 '$notify_type' => $notif['notify_type'],
242 '$dfrn_text' => $dfrn_text,
243 '$dfrn_id' => $notif['dfrn_id'],
244 '$uid' => $notif['uid'],
245 '$intro_id' => $notif['intro_id'],
246 '$contact_id' => $notif['contact_id'],
247 '$photo' => $notif['photo'],
248 '$fullname' => $notif['name'],
249 '$location' => $notif['location'],
250 '$lbl_location'=> L10n::t('Location:'),
251 '$about' => $notif['about'],
252 '$lbl_about' => L10n::t('About:'),
253 '$keywords' => $notif['keywords'],
254 '$lbl_keywords'=> L10n::t('Tags:'),
255 '$gender' => $notif['gender'],
256 '$lbl_gender' => L10n::t('Gender:'),
257 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
258 '$url' => $notif['url'],
259 '$zrl' => $notif['zrl'],
260 '$lbl_url' => L10n::t('Profile URL'),
261 '$addr' => $notif['addr'],
262 '$lbl_knowyou' => $lbl_knowyou,
263 '$lbl_network' => L10n::t('Network:'),
264 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
265 '$knowyou' => $knowyou,
266 '$approve' => L10n::t('Approve'),
267 '$note' => $notif['note'],
268 '$ignore' => L10n::t('Ignore'),
269 '$discard' => $discard,
275 if (count($notifs['notifications']) == 0) {
276 info(L10n::t('No introductions.') . EOL);
279 // Normal notifications (no introductions)
280 } elseif (!empty($notifs['notifications'])) {
281 // Loop trough ever notification This creates an array with the output html for each
282 // notification and apply the correct template according to the notificationtype (label).
283 foreach ($notifs['notifications'] as $notif) {
284 $notification_templates = [
285 'like' => 'notifications_likes_item.tpl',
286 'dislike' => 'notifications_dislikes_item.tpl',
287 'attend' => 'notifications_attend_item.tpl',
288 'attendno' => 'notifications_attend_item.tpl',
289 'attendmaybe' => 'notifications_attend_item.tpl',
290 'friend' => 'notifications_friends_item.tpl',
291 'comment' => 'notifications_comments_item.tpl',
292 'post' => 'notifications_posts_item.tpl',
293 'notify' => 'notify.tpl',
296 $tpl_notif = get_markup_template($notification_templates[$notif['label']]);
298 $notif_content[] = replace_macros($tpl_notif, [
299 '$item_label' => $notif['label'],
300 '$item_link' => $notif['link'],
301 '$item_image' => $notif['image'],
302 '$item_url' => $notif['url'],
303 '$item_text' => $notif['text'],
304 '$item_when' => $notif['when'],
305 '$item_ago' => $notif['ago'],
306 '$item_seen' => $notif['seen'],
310 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
313 $o .= replace_macros($notif_tpl, [
314 '$notif_header' => $notif_header,
316 '$notif_content' => $notif_content,
317 '$notif_nocontent' => $notif_nocontent,
318 '$notif_show_lnk' => $notif_show_lnk,
319 '$notif_paginate' => $pager->renderMinimal(count($notif_content))