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 $intro = dba::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
29 if (DBM::is_result($intro)) {
30 $intro_id = $intro['id'];
31 $contact_id = $intro['contact-id'];
33 notice(L10n::t('Invalid request identifier.') . EOL);
37 // If it is a friend suggestion, the contact is not a new friend but an existing friend
38 // that should not be deleted.
42 if ($_POST['submit'] == L10n::t('Discard')) {
43 dba::delete('intro', ['id' => $intro_id]);
46 // The check for blocked and pending is in case the friendship was already approved
47 // and we just want to get rid of the now pointless notification
48 $condition = ['id' => $contact_id, 'uid' => local_user(),
49 'self' => false, 'blocked' => true, 'pending' => true];
50 dba::delete('contact', $condition);
52 goaway('notifications/intros');
55 if ($_POST['submit'] == L10n::t('Ignore')) {
56 dba::update('intro', ['ignore' => true], ['id' => $intro_id]);
57 goaway('notifications/intros');
62 function notifications_content(App $a)
65 notice(L10n::t('Permission denied.') . EOL);
69 $page = (x($_REQUEST,'page') ? $_REQUEST['page'] : 1);
70 $show = (x($_REQUEST,'show') ? $_REQUEST['show'] : 0);
72 Nav::setSelected('notifications');
74 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
76 $nm = new NotificationsManager();
79 // Get the nav tabs for the notification pages
80 $tabs = $nm->getTabs();
83 // Notification results per page
85 $startrec = ($page * $perpage) - $perpage;
88 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
89 Nav::setSelected('introductions');
90 $notif_header = L10n::t('Notifications');
92 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
94 $notifs = $nm->introNotifs($all, $startrec, $perpage);
96 // Get the network notifications
97 } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
98 $notif_header = L10n::t('Network Notifications');
99 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
101 // Get the system notifications
102 } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
103 $notif_header = L10n::t('System Notifications');
104 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
106 // Get the personal notifications
107 } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
108 $notif_header = L10n::t('Personal Notifications');
109 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
111 // Get the home notifications
112 } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
113 $notif_header = L10n::t('Home Notifications');
114 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
120 $a->set_pager_itemspage($perpage);
122 // Add additional informations (needed for json output)
123 $notifs['items_page'] = $a->pager['itemspage'];
124 $notifs['page'] = $a->pager['page'];
127 if (intval($json) === 1) {
128 System::jsonExit($notifs);
131 $notif_tpl = get_markup_template('notifications.tpl');
133 // Process the data for template creation
134 if ($notifs['ident'] === 'introductions') {
135 $sugg = get_markup_template('suggestions.tpl');
136 $tpl = get_markup_template("intros.tpl");
138 // The link to switch between ignored and normal connection requests
140 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
141 'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
144 // Loop through all introduction notifications.This creates an array with the output html for each
146 foreach ($notifs['notifications'] as $it) {
148 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
149 // We have to distinguish between these two because they use different data.
150 switch ($it['label']) {
151 case 'friend_suggestion':
152 $notif_content[] = replace_macros($sugg, [
153 '$type' => $it['label'],
154 '$str_notifytype' => L10n::t('Notification type:'),
155 '$notify_type' => $it['notify_type'],
156 '$intro_id' => $it['intro_id'],
157 '$lbl_madeby' => L10n::t('Suggested by:'),
158 '$madeby' => $it['madeby'],
159 '$madeby_url' => $it['madeby_url'],
160 '$madeby_zrl' => $it['madeby_zrl'],
161 '$madeby_addr' => $it['madeby_addr'],
162 '$contact_id' => $it['contact_id'],
163 '$photo' => $it['photo'],
164 '$fullname' => $it['name'],
165 '$url' => $it['url'],
166 '$zrl' => $it['zrl'],
167 '$lbl_url' => L10n::t('Profile URL'),
168 '$addr' => $it['addr'],
169 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($it['hidden'] == 1), ''],
171 '$knowyou' => $it['knowyou'],
172 '$approve' => L10n::t('Approve'),
173 '$note' => $it['note'],
174 '$request' => $it['request'],
175 '$ignore' => L10n::t('Ignore'),
176 '$discard' => L10n::t('Discard'),
180 // Normal connection requests
182 $friend_selected = (($it['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
183 $fan_selected = (($it['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
184 $dfrn_tpl = get_markup_template('netfriend.tpl');
189 if ($it['network'] === NETWORK_DFRN || $it['network'] === NETWORK_DIASPORA) {
190 if ($it['network'] === NETWORK_DFRN) {
191 $lbl_knowyou = L10n::t('Claims to be known to you: ');
192 $knowyou = (($it['knowyou']) ? L10n::t('yes') : L10n::t('no'));
193 $helptext = L10n::t('Shall your connection be bidirectional or not?');
194 $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']);
195 $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']);
198 $helptext = L10n::t('Shall your connection be bidirectional or not?');
199 $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']);
200 $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']);
204 $dfrn_text = replace_macros($dfrn_tpl,[
205 '$intro_id' => $it['intro_id'],
206 '$friend_selected' => $friend_selected,
207 '$fan_selected' => $fan_selected,
208 '$approve_as1' => $helptext,
209 '$approve_as2' => $helptext2,
210 '$approve_as3' => $helptext3,
211 '$as_friend' => L10n::t('Friend'),
212 '$as_fan' => (($it['network'] == NETWORK_DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
215 $header = $it["name"];
217 if ($it["addr"] != "") {
218 $header .= " <".$it["addr"].">";
221 $header .= " (".ContactSelector::networkToName($it['network'], $it['url']).")";
223 if ($it['network'] != NETWORK_DIASPORA) {
224 $discard = L10n::t('Discard');
229 $notif_content[] = replace_macros($tpl, [
230 '$type' => $it['label'],
231 '$header' => htmlentities($header),
232 '$str_notifytype' => L10n::t('Notification type:'),
233 '$notify_type' => $it['notify_type'],
234 '$dfrn_text' => $dfrn_text,
235 '$dfrn_id' => $it['dfrn_id'],
236 '$uid' => $it['uid'],
237 '$intro_id' => $it['intro_id'],
238 '$contact_id' => $it['contact_id'],
239 '$photo' => $it['photo'],
240 '$fullname' => $it['name'],
241 '$location' => $it['location'],
242 '$lbl_location' => L10n::t('Location:'),
243 '$about' => $it['about'],
244 '$lbl_about' => L10n::t('About:'),
245 '$keywords' => $it['keywords'],
246 '$lbl_keywords' => L10n::t('Tags:'),
247 '$gender' => $it['gender'],
248 '$lbl_gender' => L10n::t('Gender:'),
249 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($it['hidden'] == 1), ''],
250 '$url' => $it['url'],
251 '$zrl' => $it['zrl'],
252 '$lbl_url' => L10n::t('Profile URL'),
253 '$addr' => $it['addr'],
254 '$lbl_knowyou' => $lbl_knowyou,
255 '$lbl_network' => L10n::t('Network:'),
256 '$network' => ContactSelector::networkToName($it['network'], $it['url']),
257 '$knowyou' => $knowyou,
258 '$approve' => L10n::t('Approve'),
259 '$note' => $it['note'],
260 '$ignore' => L10n::t('Ignore'),
261 '$discard' => $discard,
268 if (count($notifs['notifications']) == 0) {
269 info(L10n::t('No introductions.') . EOL);
272 // Normal notifications (no introductions)
274 // The template files we need in different cases for formatting the content
275 $tpl_item_like = 'notifications_likes_item.tpl';
276 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
277 $tpl_item_attend = 'notifications_attend_item.tpl';
278 $tpl_item_attendno = 'notifications_attend_item.tpl';
279 $tpl_item_attendmaybe = 'notifications_attend_item.tpl';
280 $tpl_item_friend = 'notifications_friends_item.tpl';
281 $tpl_item_comment = 'notifications_comments_item.tpl';
282 $tpl_item_post = 'notifications_posts_item.tpl';
283 $tpl_item_notify = 'notify.tpl';
285 // Loop trough ever notification This creates an array with the output html for each
286 // notification and apply the correct template according to the notificationtype (label).
287 foreach ($notifs['notifications'] as $it) {
289 // We use the notification label to get the correct template file
290 $tpl_var_name = 'tpl_item_'.$it['label'];
291 $tpl_notif = get_markup_template($$tpl_var_name);
293 $notif_content[] = replace_macros($tpl_notif,[
294 '$item_label' => $it['label'],
295 '$item_link' => $it['link'],
296 '$item_image' => $it['image'],
297 '$item_url' => $it['url'],
298 '$item_text' => $it['text'],
299 '$item_when' => $it['when'],
300 '$item_ago' => $it['ago'],
301 '$item_seen' => $it['seen'],
306 'href' => ($show ? 'notifications/'.$notifs['ident'] : 'notifications/'.$notifs['ident'].'?show=all' ),
307 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
310 // Output if there aren't any notifications available
311 if (count($notifs['notifications']) == 0) {
312 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
316 $o .= replace_macros($notif_tpl, [
317 '$notif_header' => $notif_header,
319 '$notif_content' => $notif_content,
320 '$notif_nocontent' => $notif_nocontent,
321 '$notif_show_lnk' => $notif_show_lnk,
322 '$notif_paginate' => alt_pager($a, count($notif_content))