3 * @file mod/notifications.php
4 * @brief The notifications module
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Nav;
9 use Friendica\Core\NotificationsManager;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
13 require_once "include/network.php";
15 function notifications_post(App $a) {
18 goaway(System::baseUrl());
21 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
23 if($request_id === "all")
28 $r = q("SELECT * FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
33 if (DBM::is_result($r)) {
34 $intro_id = $r[0]['id'];
35 $contact_id = $r[0]['contact-id'];
38 notice( 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'] == t('Discard')) {
48 $r = q("DELETE FROM `intro` WHERE `id` = %d",
53 // The check for blocked and pending is in case the friendship was already approved
54 // and we just want to get rid of the now pointless notification
56 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 AND `blocked` = 1 AND `pending` = 1",
61 goaway('notifications/intros');
63 if($_POST['submit'] == t('Ignore')) {
64 $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d",
66 goaway('notifications/intros');
71 function notifications_content(App $a) {
74 notice( t('Permission denied.') . EOL);
78 $page = (x($_REQUEST,'page') ? $_REQUEST['page'] : 1);
79 $show = (x($_REQUEST,'show') ? $_REQUEST['show'] : 0);
81 Nav::setSelected('notifications');
83 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
85 $nm = new NotificationsManager();
88 // Get the nav tabs for the notification pages
89 $tabs = $nm->getTabs();
92 // Notification results per page
94 $startrec = ($page * $perpage) - $perpage;
97 if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
98 Nav::setSelected('introductions');
99 $notif_header = t('Notifications');
101 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
103 $notifs = $nm->introNotifs($all, $startrec, $perpage);
105 // Get the network notifications
106 } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
108 $notif_header = t('Network Notifications');
109 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
111 // Get the system notifications
112 } else if (($a->argc > 1) && ($a->argv[1] == 'system')) {
114 $notif_header = t('System Notifications');
115 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
117 // Get the personal notifications
118 } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
120 $notif_header = t('Personal Notifications');
121 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
123 // Get the home notifications
124 } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
126 $notif_header = t('Home Notifications');
127 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
133 $a->set_pager_total($notifs['total']);
134 $a->set_pager_itemspage($perpage);
136 // Add additional informations (needed for json output)
137 $notifs['items_page'] = $a->pager['itemspage'];
138 $notifs['page'] = $a->pager['page'];
141 if(intval($json) === 1)
142 json_return_and_die($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 ? t('Show Ignored Requests') : 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 '$str_notifytype' => t('Notification type: '),
168 '$notify_type' => $it['notify_type'],
169 '$intro_id' => $it['intro_id'],
170 '$madeby' => sprintf( t('suggested by %s'),$it['madeby']),
171 '$contact_id' => $it['contact-id'],
172 '$photo' => $it['photo'],
173 '$fullname' => $it['name'],
174 '$url' => $it['url'],
175 '$hidden' => ['hidden', t('Hide this contact from others'), ($it['hidden'] == 1), ''],
176 '$activity' => ['activity', t('Post a new friend activity'), $it['post_newfriend'], t('if applicable')],
178 '$knowyou' => $it['knowyou'],
179 '$approve' => t('Approve'),
180 '$note' => $it['note'],
181 '$request' => $it['request'],
182 '$ignore' => t('Ignore'),
183 '$discard' => t('Discard'),
187 // Normal connection requests
189 $friend_selected = (($it['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
190 $fan_selected = (($it['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
191 $dfrn_tpl = get_markup_template('netfriend.tpl');
196 if($it['network'] === NETWORK_DFRN || $it['network'] === NETWORK_DIASPORA) {
197 if($it['network'] === NETWORK_DFRN) {
198 $lbl_knowyou = t('Claims to be known to you: ');
199 $knowyou = (($it['knowyou']) ? t('yes') : t('no'));
200 $helptext = t('Shall your connection be bidirectional or not?');
201 $helptext2 = sprintf(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']);
202 $helptext3 = sprintf(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']);
205 $helptext = t('Shall your connection be bidirectional or not?');
206 $helptext2 = sprintf(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']);
207 $helptext3 = sprintf(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']);
211 $dfrn_text = replace_macros($dfrn_tpl,[
212 '$intro_id' => $it['intro_id'],
213 '$friend_selected' => $friend_selected,
214 '$fan_selected' => $fan_selected,
215 '$approve_as1' => $helptext,
216 '$approve_as2' => $helptext2,
217 '$approve_as3' => $helptext3,
218 '$as_friend' => t('Friend'),
219 '$as_fan' => (($it['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Subscriber'))
222 $header = $it["name"];
224 if ($it["addr"] != "")
225 $header .= " <".$it["addr"].">";
227 $header .= " (".ContactSelector::networkToName($it['network'], $it['url']).")";
229 if ($it['network'] != NETWORK_DIASPORA) {
230 $discard = t('Discard');
235 $notif_content[] = replace_macros($tpl, [
236 '$header' => htmlentities($header),
237 '$str_notifytype' => t('Notification type: '),
238 '$notify_type' => $it['notify_type'],
239 '$dfrn_text' => $dfrn_text,
240 '$dfrn_id' => $it['dfrn_id'],
241 '$uid' => $it['uid'],
242 '$intro_id' => $it['intro_id'],
243 '$contact_id' => $it['contact_id'],
244 '$photo' => $it['photo'],
245 '$fullname' => $it['name'],
246 '$location' => $it['location'],
247 '$lbl_location' => t('Location:'),
248 '$about' => $it['about'],
249 '$lbl_about' => t('About:'),
250 '$keywords' => $it['keywords'],
251 '$lbl_keywords' => t('Tags:'),
252 '$gender' => $it['gender'],
253 '$lbl_gender' => t('Gender:'),
254 '$hidden' => ['hidden', t('Hide this contact from others'), ($it['hidden'] == 1), ''],
255 '$activity' => ['activity', t('Post a new friend activity'), $it['post_newfriend'], t('if applicable')],
256 '$url' => $it['url'],
257 '$zrl' => $it['zrl'],
258 '$lbl_url' => t('Profile URL'),
259 '$addr' => $it['addr'],
260 '$lbl_knowyou' => $lbl_knowyou,
261 '$lbl_network' => t('Network:'),
262 '$network' => ContactSelector::networkToName($it['network'], $it['url']),
263 '$knowyou' => $knowyou,
264 '$approve' => t('Approve'),
265 '$note' => $it['note'],
266 '$ignore' => t('Ignore'),
267 '$discard' => $discard,
274 if($notifs['total'] == 0)
275 info( t('No introductions.') . EOL);
277 // Normal notifications (no introductions)
280 // The template files we need in different cases for formatting the content
281 $tpl_item_like = 'notifications_likes_item.tpl';
282 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
283 $tpl_item_attend = 'notifications_attend_item.tpl';
284 $tpl_item_attendno = 'notifications_attend_item.tpl';
285 $tpl_item_attendmaybe = 'notifications_attend_item.tpl';
286 $tpl_item_friend = 'notifications_friends_item.tpl';
287 $tpl_item_comment = 'notifications_comments_item.tpl';
288 $tpl_item_post = 'notifications_posts_item.tpl';
289 $tpl_item_notify = 'notify.tpl';
291 // Loop trough ever notification This creates an array with the output html for each
292 // notification and apply the correct template according to the notificationtype (label).
293 foreach ($notifs['notifications'] as $it) {
295 // We use the notification label to get the correct template file
296 $tpl_var_name = 'tpl_item_'.$it['label'];
297 $tpl_notif = get_markup_template($$tpl_var_name);
299 $notif_content[] = replace_macros($tpl_notif,[
300 '$item_label' => $it['label'],
301 '$item_link' => $it['link'],
302 '$item_image' => $it['image'],
303 '$item_url' => $it['url'],
304 '$item_text' => $it['text'],
305 '$item_when' => $it['when'],
306 '$item_ago' => $it['ago'],
307 '$item_seen' => $it['seen'],
311 // It doesn't make sense to show the Show unread / Show all link visible if the user is on the
312 // "Show all" page and there are no notifications. So we will hide it.
313 if($show == 0 || intval($show) && $notifs['total'] > 0) {
315 'href' => ($show ? 'notifications/'.$notifs['ident'] : 'notifications/'.$notifs['ident'].'?show=all' ),
316 'text' => ($show ? t('Show unread') : t('Show all')),
320 // Output if there aren't any notifications available
321 if($notifs['total'] == 0)
322 $notif_nocontent = sprintf( t('No more %s notifications.'), $notifs['ident']);
325 $o .= replace_macros($notif_tpl, [
326 '$notif_header' => $notif_header,
328 '$notif_content' => $notif_content,
329 '$notif_nocontent' => $notif_nocontent,
330 '$notif_show_lnk' => $notif_show_lnk,
331 '$notif_paginate' => paginate($a)