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\Renderer;
15 use Friendica\Core\System;
16 use Friendica\Database\DBA;
17 use Friendica\Module\Login;
19 function notifications_post(App $a)
22 $a->internalRedirect();
25 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
27 if ($request_id === 'all') {
32 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
34 if (DBA::isResult($intro)) {
35 $intro_id = $intro['id'];
36 $contact_id = $intro['contact-id'];
38 notice(L10n::t('Invalid request identifier.') . EOL);
42 // If it is a friend suggestion, the contact is not a new friend but an existing friend
43 // that should not be deleted.
47 if ($_POST['submit'] == L10n::t('Discard')) {
48 DBA::delete('intro', ['id' => $intro_id]);
51 // The check for blocked and pending is in case the friendship was already approved
52 // and we just want to get rid of the now pointless notification
53 $condition = ['id' => $contact_id, 'uid' => local_user(),
54 'self' => false, 'blocked' => true, 'pending' => true];
55 DBA::delete('contact', $condition);
57 $a->internalRedirect('notifications/intros');
60 if ($_POST['submit'] == L10n::t('Ignore')) {
61 DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
62 $a->internalRedirect('notifications/intros');
67 function notifications_content(App $a)
70 notice(L10n::t('Permission denied.') . EOL);
74 $page = defaults($_REQUEST, 'page', 1);
75 $show = defaults($_REQUEST, 'show', 0);
77 Nav::setSelected('notifications');
79 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
81 $nm = new NotificationsManager();
84 // Get the nav tabs for the notification pages
85 $tabs = $nm->getTabs();
87 $notif_nocontent = '';
89 // Notification results per page
91 $startrec = ($page * $perpage) - $perpage;
93 $notif_header = L10n::t('Notifications');
96 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
97 Nav::setSelected('introductions');
99 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
101 $notifs = $nm->introNotifs($all, $startrec, $perpage);
103 // Get the network notifications
104 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
105 $notif_header = L10n::t('Network Notifications');
106 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
108 // Get the system notifications
109 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
110 $notif_header = L10n::t('System Notifications');
111 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
113 // Get the personal notifications
114 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
115 $notif_header = L10n::t('Personal Notifications');
116 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
118 // Get the home notifications
119 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
120 $notif_header = L10n::t('Home Notifications');
121 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
125 $pager = new Pager($a->query_string, $perpage);
127 // Add additional informations (needed for json output)
128 $notifs['items_page'] = $pager->getItemsPerPage();
129 $notifs['page'] = $pager->getPage();
132 if (intval($json) === 1) {
133 System::jsonExit($notifs);
136 $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
139 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
140 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
143 // Process the data for template creation
144 if (defaults($notifs, 'ident', '') === 'introductions') {
145 $sugg = Renderer::getMarkupTemplate('suggestions.tpl');
146 $tpl = Renderer::getMarkupTemplate('intros.tpl');
148 // The link to switch between ignored and normal connection requests
150 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
151 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
154 // Loop through all introduction notifications.This creates an array with the output html for each
156 foreach ($notifs['notifications'] as $notif) {
158 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
159 // We have to distinguish between these two because they use different data.
160 switch ($notif['label']) {
161 case 'friend_suggestion':
162 $notif_content[] = Renderer::replaceMacros($sugg, [
163 '$type' => $notif['label'],
164 '$str_notifytype' => L10n::t('Notification type:'),
165 '$notify_type'=> $notif['notify_type'],
166 '$intro_id' => $notif['intro_id'],
167 '$lbl_madeby' => L10n::t('Suggested by:'),
168 '$madeby' => $notif['madeby'],
169 '$madeby_url' => $notif['madeby_url'],
170 '$madeby_zrl' => $notif['madeby_zrl'],
171 '$madeby_addr'=> $notif['madeby_addr'],
172 '$contact_id' => $notif['contact_id'],
173 '$photo' => $notif['photo'],
174 '$fullname' => $notif['name'],
175 '$url' => $notif['url'],
176 '$zrl' => $notif['zrl'],
177 '$lbl_url' => L10n::t('Profile URL'),
178 '$addr' => $notif['addr'],
179 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
180 '$knowyou' => $notif['knowyou'],
181 '$approve' => L10n::t('Approve'),
182 '$note' => $notif['note'],
183 '$request' => $notif['request'],
184 '$ignore' => L10n::t('Ignore'),
185 '$discard' => L10n::t('Discard'),
189 // Normal connection requests
191 $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
192 $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
200 if ($notif['network'] === Protocol::DFRN) {
201 $lbl_knowyou = L10n::t('Claims to be known to you: ');
202 $knowyou = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
203 $helptext = L10n::t('Shall your connection be bidirectional or not?');
204 $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']);
205 $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']);
206 } elseif ($notif['network'] === Protocol::DIASPORA) {
207 $helptext = L10n::t('Shall your connection be bidirectional or not?');
208 $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']);
209 $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']);
212 $dfrn_tpl = Renderer::getMarkupTemplate('netfriend.tpl');
213 $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [
214 '$intro_id' => $notif['intro_id'],
215 '$friend_selected' => $friend_selected,
216 '$fan_selected'=> $fan_selected,
217 '$approve_as1' => $helptext,
218 '$approve_as2' => $helptext2,
219 '$approve_as3' => $helptext3,
220 '$as_friend' => L10n::t('Friend'),
221 '$as_fan' => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
224 $header = $notif['name'];
226 if ($notif['addr'] != '') {
227 $header .= ' <' . $notif['addr'] . '>';
230 $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
232 if ($notif['network'] != Protocol::DIASPORA) {
233 $discard = L10n::t('Discard');
238 $notif_content[] = Renderer::replaceMacros($tpl, [
239 '$type' => $notif['label'],
240 '$header' => htmlentities($header),
241 '$str_notifytype' => L10n::t('Notification type:'),
242 '$notify_type' => $notif['notify_type'],
243 '$dfrn_text' => $dfrn_text,
244 '$dfrn_id' => $notif['dfrn_id'],
245 '$uid' => $notif['uid'],
246 '$intro_id' => $notif['intro_id'],
247 '$contact_id' => $notif['contact_id'],
248 '$photo' => $notif['photo'],
249 '$fullname' => $notif['name'],
250 '$location' => $notif['location'],
251 '$lbl_location'=> L10n::t('Location:'),
252 '$about' => $notif['about'],
253 '$lbl_about' => L10n::t('About:'),
254 '$keywords' => $notif['keywords'],
255 '$lbl_keywords'=> L10n::t('Tags:'),
256 '$gender' => $notif['gender'],
257 '$lbl_gender' => L10n::t('Gender:'),
258 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
259 '$url' => $notif['url'],
260 '$zrl' => $notif['zrl'],
261 '$lbl_url' => L10n::t('Profile URL'),
262 '$addr' => $notif['addr'],
263 '$lbl_knowyou' => $lbl_knowyou,
264 '$lbl_network' => L10n::t('Network:'),
265 '$network' => ContactSelector::networkToName($notif['network'], $notif['url']),
266 '$knowyou' => $knowyou,
267 '$approve' => L10n::t('Approve'),
268 '$note' => $notif['note'],
269 '$ignore' => L10n::t('Ignore'),
270 '$discard' => $discard,
276 if (count($notifs['notifications']) == 0) {
277 info(L10n::t('No introductions.') . EOL);
280 // Normal notifications (no introductions)
281 } elseif (!empty($notifs['notifications'])) {
282 // Loop trough ever notification This creates an array with the output html for each
283 // notification and apply the correct template according to the notificationtype (label).
284 foreach ($notifs['notifications'] as $notif) {
285 $notification_templates = [
286 'like' => 'notifications_likes_item.tpl',
287 'dislike' => 'notifications_dislikes_item.tpl',
288 'attend' => 'notifications_attend_item.tpl',
289 'attendno' => 'notifications_attend_item.tpl',
290 'attendmaybe' => 'notifications_attend_item.tpl',
291 'friend' => 'notifications_friends_item.tpl',
292 'comment' => 'notifications_comments_item.tpl',
293 'post' => 'notifications_posts_item.tpl',
294 'notify' => 'notify.tpl',
297 $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
299 $notif_content[] = Renderer::replaceMacros($tpl_notif, [
300 '$item_label' => $notif['label'],
301 '$item_link' => $notif['link'],
302 '$item_image' => $notif['image'],
303 '$item_url' => $notif['url'],
304 '$item_text' => $notif['text'],
305 '$item_when' => $notif['when'],
306 '$item_ago' => $notif['ago'],
307 '$item_seen' => $notif['seen'],
311 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
314 $o .= Renderer::replaceMacros($notif_tpl, [
315 '$notif_header' => $notif_header,
317 '$notif_content' => $notif_content,
318 '$notif_nocontent' => $notif_nocontent,
319 '$notif_show_lnk' => $notif_show_lnk,
320 '$notif_paginate' => $pager->renderMinimal(count($notif_content))