4 * @file mod/notifications.php
5 * @brief The notifications module
9 use Friendica\Core\NotificationsManager;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
13 require_once("include/contact_selectors.php");
14 require_once("include/network.php");
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 $r = q("SELECT * FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
34 if (DBM::is_result($r)) {
35 $intro_id = $r[0]['id'];
36 $contact_id = $r[0]['contact-id'];
39 notice( t('Invalid request identifier.') . EOL);
43 // If it is a friend suggestion, the contact is not a new friend but an existing friend
44 // that should not be deleted.
48 if($_POST['submit'] == t('Discard')) {
49 $r = q("DELETE FROM `intro` WHERE `id` = %d",
54 // The check for blocked and pending is in case the friendship was already approved
55 // and we just want to get rid of the now pointless notification
57 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 AND `blocked` = 1 AND `pending` = 1",
62 goaway('notifications/intros');
64 if($_POST['submit'] == t('Ignore')) {
65 $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d",
67 goaway('notifications/intros');
72 function notifications_content(App $a) {
75 notice( t('Permission denied.') . EOL);
79 $page = (x($_REQUEST,'page') ? $_REQUEST['page'] : 1);
80 $show = (x($_REQUEST,'show') ? $_REQUEST['show'] : 0);
82 nav_set_selected('notifications');
84 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
86 $nm = new NotificationsManager();
89 // Get the nav tabs for the notification pages
90 $tabs = $nm->getTabs();
91 $notif_content = array();
93 // Notification results per page
95 $startrec = ($page * $perpage) - $perpage;
98 if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
99 nav_set_selected('introductions');
100 $notif_header = t('Notifications');
102 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
104 $notifs = $nm->introNotifs($all, $startrec, $perpage);
106 // Get the network notifications
107 } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
109 $notif_header = t('Network Notifications');
110 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
112 // Get the system notifications
113 } else if (($a->argc > 1) && ($a->argv[1] == 'system')) {
115 $notif_header = t('System Notifications');
116 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
118 // Get the personal notifications
119 } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
121 $notif_header = t('Personal Notifications');
122 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
124 // Get the home notifications
125 } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
127 $notif_header = t('Home Notifications');
128 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
134 $a->set_pager_total($notifs['total']);
135 $a->set_pager_itemspage($perpage);
137 // Add additional informations (needed for json output)
138 $notifs['items_page'] = $a->pager['itemspage'];
139 $notifs['page'] = $a->pager['page'];
142 if(intval($json) === 1)
143 json_return_and_die($notifs);
145 $notif_tpl = get_markup_template('notifications.tpl');
147 // Process the data for template creation
148 if($notifs['ident'] === 'introductions') {
150 $sugg = get_markup_template('suggestions.tpl');
151 $tpl = get_markup_template("intros.tpl");
153 // The link to switch between ignored and normal connection requests
154 $notif_show_lnk = array(
155 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
156 'text' => (!$all ? t('Show Ignored Requests') : t('Hide Ignored Requests'))
159 // Loop through all introduction notifications.This creates an array with the output html for each
161 foreach ($notifs['notifications'] as $it) {
163 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
164 // We have to distinguish between these two because they use different data.
165 switch ($it['label']) {
166 case 'friend_suggestion':
167 $notif_content[] = replace_macros($sugg, array(
168 '$str_notifytype' => t('Notification type: '),
169 '$notify_type' => $it['notify_type'],
170 '$intro_id' => $it['intro_id'],
171 '$madeby' => sprintf( t('suggested by %s'),$it['madeby']),
172 '$contact_id' => $it['contact-id'],
173 '$photo' => $it['photo'],
174 '$fullname' => $it['name'],
175 '$url' => $it['url'],
176 '$hidden' => array('hidden', t('Hide this contact from others'), ($it['hidden'] == 1), ''),
177 '$activity' => array('activity', t('Post a new friend activity'), $it['post_newfriend'], t('if applicable')),
179 '$knowyou' => $it['knowyou'],
180 '$approve' => t('Approve'),
181 '$note' => $it['note'],
182 '$request' => $it['request'],
183 '$ignore' => t('Ignore'),
184 '$discard' => t('Discard'),
188 // Normal connection requests
190 $friend_selected = (($it['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
191 $fan_selected = (($it['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
192 $dfrn_tpl = get_markup_template('netfriend.tpl');
197 if($it['network'] === NETWORK_DFRN || $it['network'] === NETWORK_DIASPORA) {
198 if($it['network'] === NETWORK_DFRN) {
199 $lbl_knowyou = t('Claims to be known to you: ');
200 $knowyou = (($it['knowyou']) ? t('yes') : t('no'));
201 $helptext = t('Shall your connection be bidirectional or not?');
202 $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']);
203 $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']);
206 $helptext = t('Shall your connection be bidirectional or not?');
207 $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']);
208 $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']);
212 $dfrn_text = replace_macros($dfrn_tpl,array(
213 '$intro_id' => $it['intro_id'],
214 '$friend_selected' => $friend_selected,
215 '$fan_selected' => $fan_selected,
216 '$approve_as1' => $helptext,
217 '$approve_as2' => $helptext2,
218 '$approve_as3' => $helptext3,
219 '$as_friend' => t('Friend'),
220 '$as_fan' => (($it['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Subscriber'))
223 $header = $it["name"];
225 if ($it["addr"] != "")
226 $header .= " <".$it["addr"].">";
228 $header .= " (".network_to_name($it['network'], $it['url']).")";
230 if ($it['network'] != NETWORK_DIASPORA) {
231 $discard = t('Discard');
236 $notif_content[] = replace_macros($tpl, array(
237 '$header' => htmlentities($header),
238 '$str_notifytype' => t('Notification type: '),
239 '$notify_type' => $it['notify_type'],
240 '$dfrn_text' => $dfrn_text,
241 '$dfrn_id' => $it['dfrn_id'],
242 '$uid' => $it['uid'],
243 '$intro_id' => $it['intro_id'],
244 '$contact_id' => $it['contact_id'],
245 '$photo' => $it['photo'],
246 '$fullname' => $it['name'],
247 '$location' => $it['location'],
248 '$lbl_location' => t('Location:'),
249 '$about' => $it['about'],
250 '$lbl_about' => t('About:'),
251 '$keywords' => $it['keywords'],
252 '$lbl_keywords' => t('Tags:'),
253 '$gender' => $it['gender'],
254 '$lbl_gender' => t('Gender:'),
255 '$hidden' => array('hidden', t('Hide this contact from others'), ($it['hidden'] == 1), ''),
256 '$activity' => array('activity', t('Post a new friend activity'), $it['post_newfriend'], t('if applicable')),
257 '$url' => $it['url'],
258 '$zrl' => $it['zrl'],
259 '$lbl_url' => t('Profile URL'),
260 '$addr' => $it['addr'],
261 '$lbl_knowyou' => $lbl_knowyou,
262 '$lbl_network' => t('Network:'),
263 '$network' => network_to_name($it['network'], $it['url']),
264 '$knowyou' => $knowyou,
265 '$approve' => t('Approve'),
266 '$note' => $it['note'],
267 '$ignore' => t('Ignore'),
268 '$discard' => $discard,
275 if($notifs['total'] == 0)
276 info( t('No introductions.') . EOL);
278 // Normal notifications (no introductions)
281 // The template files we need in different cases for formatting the content
282 $tpl_item_like = 'notifications_likes_item.tpl';
283 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
284 $tpl_item_attend = 'notifications_attend_item.tpl';
285 $tpl_item_attendno = 'notifications_attend_item.tpl';
286 $tpl_item_attendmaybe = 'notifications_attend_item.tpl';
287 $tpl_item_friend = 'notifications_friends_item.tpl';
288 $tpl_item_comment = 'notifications_comments_item.tpl';
289 $tpl_item_post = 'notifications_posts_item.tpl';
290 $tpl_item_notify = 'notify.tpl';
292 // Loop trough ever notification This creates an array with the output html for each
293 // notification and apply the correct template according to the notificationtype (label).
294 foreach ($notifs['notifications'] as $it) {
296 // We use the notification label to get the correct template file
297 $tpl_var_name = 'tpl_item_'.$it['label'];
298 $tpl_notif = get_markup_template($$tpl_var_name);
300 $notif_content[] = replace_macros($tpl_notif,array(
301 '$item_label' => $it['label'],
302 '$item_link' => $it['link'],
303 '$item_image' => $it['image'],
304 '$item_url' => $it['url'],
305 '$item_text' => $it['text'],
306 '$item_when' => $it['when'],
307 '$item_ago' => $it['ago'],
308 '$item_seen' => $it['seen'],
312 // It doesn't make sense to show the Show unread / Show all link visible if the user is on the
313 // "Show all" page and there are no notifications. So we will hide it.
314 if($show == 0 || intval($show) && $notifs['total'] > 0) {
315 $notif_show_lnk = array(
316 'href' => ($show ? 'notifications/'.$notifs['ident'] : 'notifications/'.$notifs['ident'].'?show=all' ),
317 'text' => ($show ? t('Show unread') : t('Show all')),
321 // Output if there aren't any notifications available
322 if($notifs['total'] == 0)
323 $notif_nocontent = sprintf( t('No more %s notifications.'), $notifs['ident']);
326 $o .= replace_macros($notif_tpl, array(
327 '$notif_header' => $notif_header,
329 '$notif_content' => $notif_content,
330 '$notif_nocontent' => $notif_nocontent,
331 '$notif_show_lnk' => $notif_show_lnk,
332 '$notif_paginate' => paginate($a)