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 = (x($_REQUEST,'page') ? $_REQUEST['page'] : 1);
72 $show = (x($_REQUEST,'show') ? $_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;
91 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
92 Nav::setSelected('introductions');
93 $notif_header = L10n::t('Notifications');
95 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
97 $notifs = $nm->introNotifs($all, $startrec, $perpage);
99 // Get the network notifications
100 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
101 $notif_header = L10n::t('Network Notifications');
102 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
104 // Get the system notifications
105 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
106 $notif_header = L10n::t('System Notifications');
107 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
109 // Get the personal notifications
110 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
111 $notif_header = L10n::t('Personal Notifications');
112 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
114 // Get the home notifications
115 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
116 $notif_header = L10n::t('Home Notifications');
117 $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');
136 if (!isset($notifs['ident'])) {
137 logger('Missing data in notifs: ' . System::callstack(20), LOGGER_DEBUG);
140 // Process the data for template creation
141 if ($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 $it) {
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 ($it['label']) {
158 case 'friend_suggestion':
159 $notif_content[] = replace_macros($sugg, [
160 '$type' => $it['label'],
161 '$str_notifytype' => L10n::t('Notification type:'),
162 '$notify_type' => $it['notify_type'],
163 '$intro_id' => $it['intro_id'],
164 '$lbl_madeby' => L10n::t('Suggested by:'),
165 '$madeby' => $it['madeby'],
166 '$madeby_url' => $it['madeby_url'],
167 '$madeby_zrl' => $it['madeby_zrl'],
168 '$madeby_addr' => $it['madeby_addr'],
169 '$contact_id' => $it['contact_id'],
170 '$photo' => $it['photo'],
171 '$fullname' => $it['name'],
172 '$url' => $it['url'],
173 '$zrl' => $it['zrl'],
174 '$lbl_url' => L10n::t('Profile URL'),
175 '$addr' => $it['addr'],
176 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($it['hidden'] == 1), ''],
178 '$knowyou' => $it['knowyou'],
179 '$approve' => L10n::t('Approve'),
180 '$note' => $it['note'],
181 '$request' => $it['request'],
182 '$ignore' => L10n::t('Ignore'),
183 '$discard' => L10n::t('Discard'),
187 // Normal connection requests
189 $friend_selected = (($it['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
190 $fan_selected = (($it['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
191 $dfrn_tpl = get_markup_template('netfriend.tpl');
200 if ($it['network'] === Protocol::DFRN || $it['network'] === Protocol::DIASPORA) {
201 if ($it['network'] === Protocol::DFRN) {
202 $lbl_knowyou = L10n::t('Claims to be known to you: ');
203 $knowyou = (($it['knowyou']) ? L10n::t('yes') : L10n::t('no'));
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.', $it['name'], $it['name']);
206 $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.', $it['name']);
209 $helptext = L10n::t('Shall your connection be bidirectional or not?');
210 $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.', $it['name'], $it['name']);
211 $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.', $it['name']);
215 $dfrn_text = replace_macros($dfrn_tpl,[
216 '$intro_id' => $it['intro_id'],
217 '$friend_selected' => $friend_selected,
218 '$fan_selected' => $fan_selected,
219 '$approve_as1' => $helptext,
220 '$approve_as2' => $helptext2,
221 '$approve_as3' => $helptext3,
222 '$as_friend' => L10n::t('Friend'),
223 '$as_fan' => (($it['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
226 $header = $it["name"];
228 if ($it["addr"] != "") {
229 $header .= " <".$it["addr"].">";
232 $header .= " (".ContactSelector::networkToName($it['network'], $it['url']).")";
234 if ($it['network'] != Protocol::DIASPORA) {
235 $discard = L10n::t('Discard');
240 $notif_content[] = replace_macros($tpl, [
241 '$type' => $it['label'],
242 '$header' => htmlentities($header),
243 '$str_notifytype' => L10n::t('Notification type:'),
244 '$notify_type' => $it['notify_type'],
245 '$dfrn_text' => $dfrn_text,
246 '$dfrn_id' => $it['dfrn_id'],
247 '$uid' => $it['uid'],
248 '$intro_id' => $it['intro_id'],
249 '$contact_id' => $it['contact_id'],
250 '$photo' => $it['photo'],
251 '$fullname' => $it['name'],
252 '$location' => $it['location'],
253 '$lbl_location' => L10n::t('Location:'),
254 '$about' => $it['about'],
255 '$lbl_about' => L10n::t('About:'),
256 '$keywords' => $it['keywords'],
257 '$lbl_keywords' => L10n::t('Tags:'),
258 '$gender' => $it['gender'],
259 '$lbl_gender' => L10n::t('Gender:'),
260 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($it['hidden'] == 1), ''],
261 '$url' => $it['url'],
262 '$zrl' => $it['zrl'],
263 '$lbl_url' => L10n::t('Profile URL'),
264 '$addr' => $it['addr'],
265 '$lbl_knowyou' => $lbl_knowyou,
266 '$lbl_network' => L10n::t('Network:'),
267 '$network' => ContactSelector::networkToName($it['network'], $it['url']),
268 '$knowyou' => $knowyou,
269 '$approve' => L10n::t('Approve'),
270 '$note' => $it['note'],
271 '$ignore' => L10n::t('Ignore'),
272 '$discard' => $discard,
279 if (count($notifs['notifications']) == 0) {
280 info(L10n::t('No introductions.') . EOL);
283 // Normal notifications (no introductions)
285 // The template files we need in different cases for formatting the content
286 $tpl_item_like = 'notifications_likes_item.tpl';
287 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
288 $tpl_item_attend = 'notifications_attend_item.tpl';
289 $tpl_item_attendno = 'notifications_attend_item.tpl';
290 $tpl_item_attendmaybe = 'notifications_attend_item.tpl';
291 $tpl_item_friend = 'notifications_friends_item.tpl';
292 $tpl_item_comment = 'notifications_comments_item.tpl';
293 $tpl_item_post = 'notifications_posts_item.tpl';
294 $tpl_item_notify = 'notify.tpl';
296 // Loop trough ever notification This creates an array with the output html for each
297 // notification and apply the correct template according to the notificationtype (label).
298 foreach ($notifs['notifications'] as $it) {
300 // We use the notification label to get the correct template file
301 $tpl_var_name = 'tpl_item_'.$it['label'];
302 $tpl_notif = get_markup_template($$tpl_var_name);
304 $notif_content[] = replace_macros($tpl_notif,[
305 '$item_label' => $it['label'],
306 '$item_link' => $it['link'],
307 '$item_image' => $it['image'],
308 '$item_url' => $it['url'],
309 '$item_text' => $it['text'],
310 '$item_when' => $it['when'],
311 '$item_ago' => $it['ago'],
312 '$item_seen' => $it['seen'],
317 'href' => ($show ? 'notifications/'.$notifs['ident'] : 'notifications/'.$notifs['ident'].'?show=all' ),
318 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
321 // Output if there aren't any notifications available
322 if (count($notifs['notifications']) == 0) {
323 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
327 $o .= replace_macros($notif_tpl, [
328 '$notif_header' => $notif_header,
330 '$notif_content' => $notif_content,
331 '$notif_nocontent' => $notif_nocontent,
332 '$notif_show_lnk' => $notif_show_lnk,
333 '$notif_paginate' => alt_pager($a, count($notif_content))