4 * @file mod/notifications.php
5 * @brief The notifications module
8 require_once("include/NotificationsManager.php");
9 require_once("include/contact_selectors.php");
10 require_once("include/network.php");
12 function notifications_post(App $a) {
18 $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
20 if($request_id === "all")
25 $r = q("SELECT * FROM `intro` WHERE `id` = %d AND `uid` = %d LIMIT 1",
30 if (dbm::is_result($r)) {
31 $intro_id = $r[0]['id'];
32 $contact_id = $r[0]['contact-id'];
35 notice( 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'] == t('Discard')) {
45 $r = q("DELETE FROM `intro` WHERE `id` = %d",
50 // The check for blocked and pending is in case the friendship was already approved
51 // and we just want to get rid of the now pointless notification
53 $r = q("DELETE FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 AND `blocked` = 1 AND `pending` = 1",
58 goaway('notifications/intros');
60 if($_POST['submit'] == t('Ignore')) {
61 $r = q("UPDATE `intro` SET `ignore` = 1 WHERE `id` = %d",
63 goaway('notifications/intros');
68 function notifications_content(App $a) {
71 notice( t('Permission denied.') . EOL);
75 $page = (x($_REQUEST,'page') ? $_REQUEST['page'] : 1);
76 $show = (x($_REQUEST,'show') ? $_REQUEST['show'] : 0);
78 nav_set_selected('notifications');
80 $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
82 $nm = new NotificationsManager();
85 // Get the nav tabs for the notification pages
86 $tabs = $nm->getTabs();
87 $notif_content = array();
89 // Notification results per page
91 $startrec = ($page * $perpage) - $perpage;
94 if( (($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
95 nav_set_selected('introductions');
96 $notif_header = t('Notifications');
98 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
100 $notifs = $nm->introNotifs($all, $startrec, $perpage);
102 // Get the network notifications
103 } else if (($a->argc > 1) && ($a->argv[1] == 'network')) {
105 $notif_header = t('Network Notifications');
106 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
108 // Get the system notifications
109 } else if (($a->argc > 1) && ($a->argv[1] == 'system')) {
111 $notif_header = t('System Notifications');
112 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
114 // Get the personal notifications
115 } else if (($a->argc > 1) && ($a->argv[1] == 'personal')) {
117 $notif_header = t('Personal Notifications');
118 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
120 // Get the home notifications
121 } else if (($a->argc > 1) && ($a->argv[1] == 'home')) {
123 $notif_header = t('Home Notifications');
124 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
130 $a->set_pager_total($notifs['total']);
131 $a->set_pager_itemspage($perpage);
133 // Add additional informations (needed for json output)
134 $notifs['items_page'] = $a->pager['itemspage'];
135 $notifs['page'] = $a->pager['page'];
138 if(intval($json) === 1)
139 json_return_and_die($notifs);
141 $notif_tpl = get_markup_template('notifications.tpl');
143 // Process the data for template creation
144 if($notifs['ident'] === 'introductions') {
146 $sugg = get_markup_template('suggestions.tpl');
147 $tpl = get_markup_template("intros.tpl");
149 // The link to switch between ignored and normal connection requests
150 $notif_show_lnk = array(
151 'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
152 'text' => (!$all ? t('Show Ignored Requests') : t('Hide Ignored Requests'))
155 // Loop through all introduction notifications.This creates an array with the output html for each
157 foreach ($notifs['notifications'] as $it) {
159 // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
160 // We have to distinguish between these two because they use different data.
161 switch ($it['label']) {
162 case 'friend_suggestion':
163 $notif_content[] = replace_macros($sugg, array(
164 '$str_notifytype' => t('Notification type: '),
165 '$notify_type' => $it['notify_type'],
166 '$intro_id' => $it['intro_id'],
167 '$madeby' => sprintf( t('suggested by %s'),$it['madeby']),
168 '$contact_id' => $it['contact-id'],
169 '$photo' => $it['photo'],
170 '$fullname' => $it['name'],
171 '$url' => $it['url'],
172 '$hidden' => array('hidden', t('Hide this contact from others'), ($it['hidden'] == 1), ''),
173 '$activity' => array('activity', t('Post a new friend activity'), $it['post_newfriend'], t('if applicable')),
175 '$knowyou' => $it['knowyou'],
176 '$approve' => t('Approve'),
177 '$note' => $it['note'],
178 '$request' => $it['request'],
179 '$ignore' => t('Ignore'),
180 '$discard' => t('Discard'),
184 // Normal connection requests
186 $friend_selected = (($it['network'] !== NETWORK_OSTATUS) ? ' checked="checked" ' : ' disabled ');
187 $fan_selected = (($it['network'] === NETWORK_OSTATUS) ? ' checked="checked" disabled ' : '');
188 $dfrn_tpl = get_markup_template('netfriend.tpl');
193 if($it['network'] === NETWORK_DFRN || $it['network'] === NETWORK_DIASPORA) {
194 if($it['network'] === NETWORK_DFRN) {
195 $lbl_knowyou = t('Claims to be known to you: ');
196 $knowyou = (($it['knowyou']) ? t('yes') : t('no'));
197 $helptext = t('Shall your connection be bidirectional or not?');
198 $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']);
199 $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']);
202 $helptext = t('Shall your connection be bidirectional or not?');
203 $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']);
204 $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']);
208 $dfrn_text = replace_macros($dfrn_tpl,array(
209 '$intro_id' => $it['intro_id'],
210 '$friend_selected' => $friend_selected,
211 '$fan_selected' => $fan_selected,
212 '$approve_as1' => $helptext,
213 '$approve_as2' => $helptext2,
214 '$approve_as3' => $helptext3,
215 '$as_friend' => t('Friend'),
216 '$as_fan' => (($it['network'] == NETWORK_DIASPORA) ? t('Sharer') : t('Subscriber'))
219 $header = $it["name"];
221 if ($it["addr"] != "")
222 $header .= " <".$it["addr"].">";
224 $header .= " (".network_to_name($it['network'], $it['url']).")";
226 $notif_content[] = replace_macros($tpl, array(
227 '$header' => htmlentities($header),
228 '$str_notifytype' => t('Notification type: '),
229 '$notify_type' => $it['notify_type'],
230 '$dfrn_text' => $dfrn_text,
231 '$dfrn_id' => $it['dfrn_id'],
232 '$uid' => $it['uid'],
233 '$intro_id' => $it['intro_id'],
234 '$contact_id' => $it['contact_id'],
235 '$photo' => $it['photo'],
236 '$fullname' => $it['name'],
237 '$location' => $it['location'],
238 '$lbl_location' => t('Location:'),
239 '$about' => $it['about'],
240 '$lbl_about' => t('About:'),
241 '$keywords' => $it['keywords'],
242 '$lbl_keywords' => t('Tags:'),
243 '$gender' => $it['gender'],
244 '$lbl_gender' => t('Gender:'),
245 '$hidden' => array('hidden', t('Hide this contact from others'), ($it['hidden'] == 1), ''),
246 '$activity' => array('activity', t('Post a new friend activity'), $it['post_newfriend'], t('if applicable')),
247 '$url' => $it['url'],
248 '$zrl' => $it['zrl'],
249 '$lbl_url' => t('Profile URL'),
250 '$addr' => $it['addr'],
251 '$lbl_knowyou' => $lbl_knowyou,
252 '$lbl_network' => t('Network:'),
253 '$network' => network_to_name($it['network'], $it['url']),
254 '$knowyou' => $knowyou,
255 '$approve' => t('Approve'),
256 '$note' => $it['note'],
257 '$ignore' => t('Ignore'),
258 '$discard' => t('Discard'),
265 if($notifs['total'] == 0)
266 info( t('No introductions.') . EOL);
268 // Normal notifications (no introductions)
271 // The template files we need in different cases for formatting the content
272 $tpl_item_like = 'notifications_likes_item.tpl';
273 $tpl_item_dislike = 'notifications_dislikes_item.tpl';
274 $tpl_item_attend = 'notifications_attend_item.tpl';
275 $tpl_item_attendno = 'notifications_attend_item.tpl';
276 $tpl_item_attendmaybe = 'notifications_attend_item.tpl';
277 $tpl_item_friend = 'notifications_friends_item.tpl';
278 $tpl_item_comment = 'notifications_comments_item.tpl';
279 $tpl_item_post = 'notifications_posts_item.tpl';
280 $tpl_item_notify = 'notify.tpl';
282 // Loop trough ever notification This creates an array with the output html for each
283 // notification and apply the correct template according to the notificationtype (label).
284 foreach ($notifs['notifications'] as $it) {
286 // We use the notification label to get the correct template file
287 $tpl_var_name = 'tpl_item_'.$it['label'];
288 $tpl_notif = get_markup_template($$tpl_var_name);
290 $notif_content[] = replace_macros($tpl_notif,array(
291 '$item_label' => $it['label'],
292 '$item_link' => $it['link'],
293 '$item_image' => $it['image'],
294 '$item_url' => $it['url'],
295 '$item_text' => htmlentities($it['text']),
296 '$item_when' => $it['when'],
297 '$item_ago' => $it['ago'],
298 '$item_seen' => $it['seen'],
302 // It doesn't make sense to show the Show unread / Show all link visible if the user is on the
303 // "Show all" page and there are no notifications. So we will hide it.
304 if($show == 0 || intval($show) && $notifs['total'] > 0) {
305 $notif_show_lnk = array(
306 'href' => ($show ? 'notifications/'.$notifs['ident'] : 'notifications/'.$notifs['ident'].'?show=all' ),
307 'text' => ($show ? t('Show unread') : t('Show all')),
311 // Output if there aren't any notifications available
312 if($notifs['total'] == 0)
313 $notif_nocontent = sprintf( t('No more %s notifications.'), $notifs['ident']);
316 $o .= replace_macros($notif_tpl, array(
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' => paginate($a)