5feb3be19abcb7fdd1d70ce73d91e8f60d313548
[friendica.git/.git] / mod / notifications.php
1 <?php
2 /**
3  * @file mod/notifications.php
4  * @brief The notifications module
5  */
6
7 use Friendica\App;
8 use Friendica\Content\ContactSelector;
9 use Friendica\Content\Nav;
10 use Friendica\Core\L10n;
11 use Friendica\Core\NotificationsManager;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\System;
14 use Friendica\Database\DBA;
15
16 function notifications_post(App $a)
17 {
18         if (!local_user()) {
19                 goaway(System::baseUrl());
20         }
21
22         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
23
24         if ($request_id === "all") {
25                 return;
26         }
27
28         if ($request_id) {
29                 $intro = DBA::selectFirst('intro', ['id', 'contact-id', 'fid'], ['id' => $request_id, 'uid' => local_user()]);
30
31                 if (DBA::isResult($intro)) {
32                         $intro_id = $intro['id'];
33                         $contact_id = $intro['contact-id'];
34                 } else {
35                         notice(L10n::t('Invalid request identifier.') . EOL);
36                         return;
37                 }
38
39                 // If it is a friend suggestion, the contact is not a new friend but an existing friend
40                 // that should not be deleted.
41
42                 $fid = $intro['fid'];
43
44                 if ($_POST['submit'] == L10n::t('Discard')) {
45                         DBA::delete('intro', ['id' => $intro_id]);
46
47                         if (!$fid) {
48                                 // The check for blocked and pending is in case the friendship was already approved
49                                 // and we just want to get rid of the now pointless notification
50                                 $condition = ['id' => $contact_id, 'uid' => local_user(),
51                                         'self' => false, 'blocked' => true, 'pending' => true];
52                                 DBA::delete('contact', $condition);
53                         }
54                         goaway('notifications/intros');
55                 }
56
57                 if ($_POST['submit'] == L10n::t('Ignore')) {
58                         DBA::update('intro', ['ignore' => true], ['id' => $intro_id]);
59                         goaway('notifications/intros');
60                 }
61         }
62 }
63
64 function notifications_content(App $a)
65 {
66         if (!local_user()) {
67                 notice(L10n::t('Permission denied.') . EOL);
68                 return;
69         }
70
71         $page   =       (x($_REQUEST,'page')            ? $_REQUEST['page']             : 1);
72         $show   =       (x($_REQUEST,'show')            ? $_REQUEST['show']             : 0);
73
74         Nav::setSelected('notifications');
75
76         $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
77
78         $nm = new NotificationsManager();
79
80         $o = '';
81         // Get the nav tabs for the notification pages
82         $tabs = $nm->getTabs();
83         $notif_content = [];
84         $notif_nocontent = '';
85
86         // Notification results per page
87         $perpage = 20;
88         $startrec = ($page * $perpage) - $perpage;
89
90         $notif_header = L10n::t('Notifications');
91
92         // Get introductions
93         if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
94                 Nav::setSelected('introductions');
95
96                 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
97
98                 $notifs = $nm->introNotifs($all, $startrec, $perpage);
99
100         // Get the network notifications
101         } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
102                 $notif_header = L10n::t('Network Notifications');
103                 $notifs = $nm->networkNotifs($show, $startrec, $perpage);
104
105         // Get the system notifications
106         } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
107                 $notif_header = L10n::t('System Notifications');
108                 $notifs = $nm->systemNotifs($show, $startrec, $perpage);
109
110         // Get the personal notifications
111         } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
112                 $notif_header = L10n::t('Personal Notifications');
113                 $notifs = $nm->personalNotifs($show, $startrec, $perpage);
114
115         // Get the home notifications
116         } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
117                 $notif_header = L10n::t('Home Notifications');
118                 $notifs = $nm->homeNotifs($show, $startrec, $perpage);
119         }
120
121
122         // Set the pager
123         $a->set_pager_itemspage($perpage);
124
125         // Add additional informations (needed for json output)
126         $notifs['items_page'] = $a->pager['itemspage'];
127         $notifs['page'] = $a->pager['page'];
128
129         // Json output
130         if (intval($json) === 1) {
131                 System::jsonExit($notifs);
132         }
133
134         $notif_tpl = get_markup_template('notifications.tpl');
135
136         // Process the data for template creation
137         if (defaults($notifs, 'ident', '') === 'introductions') {
138                 $sugg = get_markup_template('suggestions.tpl');
139                 $tpl = get_markup_template("intros.tpl");
140
141                 // The link to switch between ignored and normal connection requests
142                 $notif_show_lnk = [
143                         'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
144                         'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
145                 ];
146
147                 // Loop through all introduction notifications.This creates an array with the output html for each
148                 // introduction
149                 foreach ($notifs['notifications'] as $it) {
150
151                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
152                         // We have to distinguish between these two because they use different data.
153                         switch ($it['label']) {
154                                 case 'friend_suggestion':
155                                         $notif_content[] = replace_macros($sugg, [
156                                                 '$type' => $it['label'],
157                                                 '$str_notifytype' => L10n::t('Notification type:'),
158                                                 '$notify_type' => $it['notify_type'],
159                                                 '$intro_id' => $it['intro_id'],
160                                                 '$lbl_madeby' => L10n::t('Suggested by:'),
161                                                 '$madeby' => $it['madeby'],
162                                                 '$madeby_url' => $it['madeby_url'],
163                                                 '$madeby_zrl' => $it['madeby_zrl'],
164                                                 '$madeby_addr' => $it['madeby_addr'],
165                                                 '$contact_id' => $it['contact_id'],
166                                                 '$photo' => $it['photo'],
167                                                 '$fullname' => $it['name'],
168                                                 '$url' => $it['url'],
169                                                 '$zrl' => $it['zrl'],
170                                                 '$lbl_url' => L10n::t('Profile URL'),
171                                                 '$addr' => $it['addr'],
172                                                 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($it['hidden'] == 1), ''],
173
174                                                 '$knowyou' => $it['knowyou'],
175                                                 '$approve' => L10n::t('Approve'),
176                                                 '$note' => $it['note'],
177                                                 '$request' => $it['request'],
178                                                 '$ignore' => L10n::t('Ignore'),
179                                                 '$discard' => L10n::t('Discard'),
180                                         ]);
181                                         break;
182
183                                 // Normal connection requests
184                                 default:
185                                         $friend_selected = (($it['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
186                                         $fan_selected = (($it['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
187                                         $dfrn_tpl = get_markup_template('netfriend.tpl');
188
189                                         $knowyou   = '';
190                                         $lbl_knowyou = '';
191                                         $dfrn_text = '';
192                                         $helptext = '';
193                                         $helptext2 = '';
194                                         $helptext3 = '';
195
196                                         if ($it['network'] === Protocol::DFRN || $it['network'] === Protocol::DIASPORA) {
197                                                 if ($it['network'] === Protocol::DFRN) {
198                                                         $lbl_knowyou = L10n::t('Claims to be known to you: ');
199                                                         $knowyou = (($it['knowyou']) ? L10n::t('yes') : L10n::t('no'));
200                                                         $helptext = L10n::t('Shall your connection be bidirectional or not?');
201                                                         $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']);
202                                                         $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']);
203                                                 } else {
204                                                         $knowyou = '';
205                                                         $helptext = L10n::t('Shall your connection be bidirectional or not?');
206                                                         $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']);
207                                                         $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']);
208                                                 }
209                                         }
210
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' => L10n::t('Friend'),
219                                                 '$as_fan' => (($it['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
220                                         ]);
221
222                                         $header = $it["name"];
223
224                                         if ($it["addr"] != "") {
225                                                 $header .= " <".$it["addr"].">";
226                                         }
227
228                                         $header .= " (".ContactSelector::networkToName($it['network'], $it['url']).")";
229
230                                         if ($it['network'] != Protocol::DIASPORA) {
231                                                 $discard = L10n::t('Discard');
232                                         } else {
233                                                 $discard = '';
234                                         }
235
236                                         $notif_content[] = replace_macros($tpl, [
237                                                 '$type' => $it['label'],
238                                                 '$header' => htmlentities($header),
239                                                 '$str_notifytype' => L10n::t('Notification type:'),
240                                                 '$notify_type' => $it['notify_type'],
241                                                 '$dfrn_text' => $dfrn_text,
242                                                 '$dfrn_id' => $it['dfrn_id'],
243                                                 '$uid' => $it['uid'],
244                                                 '$intro_id' => $it['intro_id'],
245                                                 '$contact_id' => $it['contact_id'],
246                                                 '$photo' => $it['photo'],
247                                                 '$fullname' => $it['name'],
248                                                 '$location' => $it['location'],
249                                                 '$lbl_location' => L10n::t('Location:'),
250                                                 '$about' => $it['about'],
251                                                 '$lbl_about' => L10n::t('About:'),
252                                                 '$keywords' => $it['keywords'],
253                                                 '$lbl_keywords' => L10n::t('Tags:'),
254                                                 '$gender' => $it['gender'],
255                                                 '$lbl_gender' => L10n::t('Gender:'),
256                                                 '$hidden' => ['hidden', L10n::t('Hide this contact from others'), ($it['hidden'] == 1), ''],
257                                                 '$url' => $it['url'],
258                                                 '$zrl' => $it['zrl'],
259                                                 '$lbl_url' => L10n::t('Profile URL'),
260                                                 '$addr' => $it['addr'],
261                                                 '$lbl_knowyou' => $lbl_knowyou,
262                                                 '$lbl_network' => L10n::t('Network:'),
263                                                 '$network' => ContactSelector::networkToName($it['network'], $it['url']),
264                                                 '$knowyou' => $knowyou,
265                                                 '$approve' => L10n::t('Approve'),
266                                                 '$note' => $it['note'],
267                                                 '$ignore' => L10n::t('Ignore'),
268                                                 '$discard' => $discard,
269
270                                         ]);
271                                         break;
272                         }
273                 }
274
275                 if (count($notifs['notifications']) == 0) {
276                         info(L10n::t('No introductions.') . EOL);
277                 }
278
279         // Normal notifications (no introductions)
280         } elseif (!empty($notifs['notifications'])) {
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';
291
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) {
295
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);
299
300                         $notif_content[] = replace_macros($tpl_notif,[
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'],
309                         ]);
310                 }
311         } else {
312                 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
313         }
314
315         $notif_show_lnk = [
316                 'href' => ($show ? 'notifications/'.$notifs['ident'] : 'notifications/'.$notifs['ident'].'?show=all' ),
317                 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
318         ];
319
320         $o .= replace_macros($notif_tpl, [
321                 '$notif_header' => $notif_header,
322                 '$tabs' => $tabs,
323                 '$notif_content' => $notif_content,
324                 '$notif_nocontent' => $notif_nocontent,
325                 '$notif_show_lnk' => $notif_show_lnk,
326                 '$notif_paginate' => alt_pager($a, count($notif_content))
327         ]);
328
329         return $o;
330 }