3 * @file mod/notifications.php
4 * @brief The notifications module
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Nav;
9 use Friendica\Core\L10n;
10 use Friendica\Core\NotificationsManager;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
14 function notifications_post(App $a) {
17 goaway(System::baseUrl());
20 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
22 if($request_id === "all")
27 $r = q("SELECT * FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
32 if (DBM::is_result($r)) {
33 $intro_id = $r[0]['id'];
34 $contact_id = $r[0]['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 $r = q("DELETE FROM `intro` WHERE `id` = %d",
52 // The check for blocked and pending is in case the friendship was already approved
53 // and we just want to get rid of the now pointless notification
55 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 AND `blocked` = 1 AND `pending` = 1",
60 goaway('notifications/intros');
62 if($_POST['submit'] == L10n::t('Ignore')) {
63 $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d",
65 goaway('notifications/intros');
70 function notifications_content(App $a) {
73 notice(L10n::t('Permission denied.') . EOL);
77 $page = (x($_REQUEST,'page') ? $_REQUEST['page'] : 1);
78 $show = (x($_REQUEST,'show') ? $_REQUEST['show'] : 0);
80 Nav::setSelected('notifications');
82 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
84 $nm = new NotificationsManager();
87 // Get the nav tabs for the notification pages
88 $tabs = $nm->getTabs();
91 // Notification results per page
93 $startrec = ($page * $perpage) - $perpage;
96 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
97 Nav::setSelected('introductions');
98 $notif_header = L10n::t('Notifications');
100 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
102 $notifs = $nm->introNotifs($all, $startrec, $perpage);
104 // Get the network notifications
105 } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
107 $notif_header = L10n::t('Network Notifications');
108 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
110 // Get the system notifications
111 } else if (($a->argc > 1) && ($a->argv[1] == 'system')) {
113 $notif_header = L10n::t('System Notifications');
114 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
116 // Get the personal notifications
117 } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
119 $notif_header = L10n::t('Personal Notifications');
120 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
122 // Get the home notifications
123 } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
125 $notif_header = L10n::t('Home Notifications');
126 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
132 $a->set_pager_total($notifs['total']);
133 $a->set_pager_itemspage($perpage);
135 // Add additional informations (needed for json output)
136 $notifs['items_page'] = $a->pager['itemspage'];
137 $notifs['page'] = $a->pager['page'];
140 if (intval($json) === 1) {
141 System::jsonExit($notifs);
144 $notif_tpl = get_markup_template('notifications.tpl');
146 // Process the data for template creation
147 if($notifs['ident'] === 'introductions') {
149 $sugg = get_markup_template('suggestions.tpl');
150 $tpl = get_markup_template("intros.tpl");
152 // The link to switch between ignored and normal connection requests
154 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
155 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
158 // Loop through all introduction notifications.This creates an array with the output html for each
160 foreach ($notifs['notifications'] as $it) {
162 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
163 // We have to distinguish between these two because they use different data.
164 switch ($it['label']) {
165 case 'friend_suggestion':
166 $notif_content[] = replace_macros($sugg, [
167 '$type' => $it['label'],
168 '$str_notifytype' => L10n::t('Notification type:'),
169 '$notify_type' => $it['notify_type'],
170 '$intro_id' => $it['intro_id'],
171 '$lbl_madeby' => L10n::t('Suggested by:'),
172 '$madeby' => $it['madeby'],
173 '$madeby_url' => $it['madeby_url'],
174 '$madeby_zrl' => $it['madeby_zrl'],
175 '$madeby_addr' => $it['madeby_addr'],
176 '$contact_id' => $it['contact_id'],
177 '$photo' => $it['photo'],
178 '$fullname' => $it['name'],
179 '$url' => $it['url'],
180 '$zrl' => $it['zrl'],
181 '$lbl_url' => L10n::t('Profile URL'),
182 '$addr' => $it['addr'],
183 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($it['hidden'] == 1), ''],
185 '$knowyou' => $it['knowyou'],
186 '$approve' => L10n::t('Approve'),
187 '$note' => $it['note'],
188 '$request' => $it['request'],
189 '$ignore' => L10n::t('Ignore'),
190 '$discard' => L10n::t('Discard'),
194 // Normal connection requests
196 $friend_selected = (($it['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
197 $fan_selected = (($it['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
198 $dfrn_tpl = get_markup_template('netfriend.tpl');
203 if($it['network'] === NETWORK_DFRN || $it['network'] === NETWORK_DIASPORA) {
204 if($it['network'] === NETWORK_DFRN) {
205 $lbl_knowyou = L10n::t('Claims to be known to you: ');
206 $knowyou = (($it['knowyou']) ? L10n::t('yes') : L10n::t('no'));
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.', $it['name'], $it['name']);
209 $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']);
212 $helptext = L10n::t('Shall your connection be bidirectional or not?');
213 $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']);
214 $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']);
218 $dfrn_text = replace_macros($dfrn_tpl,[
219 '$intro_id' => $it['intro_id'],
220 '$friend_selected' => $friend_selected,
221 '$fan_selected' => $fan_selected,
222 '$approve_as1' => $helptext,
223 '$approve_as2' => $helptext2,
224 '$approve_as3' => $helptext3,
225 '$as_friend' => L10n::t('Friend'),
226 '$as_fan' => (($it['network'] == NETWORK_DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
229 $header = $it["name"];
231 if ($it["addr"] != "")
232 $header .= " <".$it["addr"].">";
234 $header .= " (".ContactSelector::networkToName($it['network'], $it['url']).")";
236 if ($it['network'] != NETWORK_DIASPORA) {
237 $discard = L10n::t('Discard');
242 $notif_content[] = replace_macros($tpl, [
243 '$type' => $it['label'],
244 '$header' => htmlentities($header),
245 '$str_notifytype' => L10n::t('Notification type:'),
246 '$notify_type' => $it['notify_type'],
247 '$dfrn_text' => $dfrn_text,
248 '$dfrn_id' => $it['dfrn_id'],
249 '$uid' => $it['uid'],
250 '$intro_id' => $it['intro_id'],
251 '$contact_id' => $it['contact_id'],
252 '$photo' => $it['photo'],
253 '$fullname' => $it['name'],
254 '$location' => $it['location'],
255 '$lbl_location' => L10n::t('Location:'),
256 '$about' => $it['about'],
257 '$lbl_about' => L10n::t('About:'),
258 '$keywords' => $it['keywords'],
259 '$lbl_keywords' => L10n::t('Tags:'),
260 '$gender' => $it['gender'],
261 '$lbl_gender' => L10n::t('Gender:'),
262 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($it['hidden'] == 1), ''],
263 '$url' => $it['url'],
264 '$zrl' => $it['zrl'],
265 '$lbl_url' => L10n::t('Profile URL'),
266 '$addr' => $it['addr'],
267 '$lbl_knowyou' => $lbl_knowyou,
268 '$lbl_network' => L10n::t('Network:'),
269 '$network' => ContactSelector::networkToName($it['network'], $it['url']),
270 '$knowyou' => $knowyou,
271 '$approve' => L10n::t('Approve'),
272 '$note' => $it['note'],
273 '$ignore' => L10n::t('Ignore'),
274 '$discard' => $discard,
281 if($notifs['total'] == 0)
282 info(L10n::t('No introductions.') . EOL);
284 // Normal notifications (no introductions)
287 // The template files we need in different cases for formatting the content
288 $tpl_item_like = 'notifications_likes_item.tpl';
289 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
290 $tpl_item_attend = 'notifications_attend_item.tpl';
291 $tpl_item_attendno = 'notifications_attend_item.tpl';
292 $tpl_item_attendmaybe = 'notifications_attend_item.tpl';
293 $tpl_item_friend = 'notifications_friends_item.tpl';
294 $tpl_item_comment = 'notifications_comments_item.tpl';
295 $tpl_item_post = 'notifications_posts_item.tpl';
296 $tpl_item_notify = 'notify.tpl';
298 // Loop trough ever notification This creates an array with the output html for each
299 // notification and apply the correct template according to the notificationtype (label).
300 foreach ($notifs['notifications'] as $it) {
302 // We use the notification label to get the correct template file
303 $tpl_var_name = 'tpl_item_'.$it['label'];
304 $tpl_notif = get_markup_template($$tpl_var_name);
306 $notif_content[] = replace_macros($tpl_notif,[
307 '$item_label' => $it['label'],
308 '$item_link' => $it['link'],
309 '$item_image' => $it['image'],
310 '$item_url' => $it['url'],
311 '$item_text' => $it['text'],
312 '$item_when' => $it['when'],
313 '$item_ago' => $it['ago'],
314 '$item_seen' => $it['seen'],
318 // It doesn't make sense to show the Show unread / Show all link visible if the user is on the
319 // "Show all" page and there are no notifications. So we will hide it.
320 if ($show == 0 || intval($show) && $notifs['total'] > 0) {
322 'href' => ($show ? 'notifications/'.$notifs['ident'] : 'notifications/'.$notifs['ident'].'?show=all' ),
323 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
327 // Output if there aren't any notifications available
328 if ($notifs['total'] == 0) {
329 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
333 $o .= replace_macros($notif_tpl, [
334 '$notif_header' => $notif_header,
336 '$notif_content' => $notif_content,
337 '$notif_nocontent' => $notif_nocontent,
338 '$notif_show_lnk' => $notif_show_lnk,
339 '$notif_paginate' => paginate($a)