]> git.mxchange.org Git - friendica.git/blob - mod/notifications.php
e083e3b44cc1631a878f3988e82b72e09dfbd886
[friendica.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\Content\Pager;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\DI;
17 use Friendica\Module\Security\Login;
18 use Friendica\Model\Contact;
19 use Friendica\Model\Introduction;
20 use Friendica\Model\Notify;
21
22 function notifications_post(App $a)
23 {
24         if (!local_user()) {
25                 $a->internalRedirect();
26         }
27
28         $request_id = (($a->argc > 1) ? $a->argv[1] : 0);
29
30         if ($request_id === 'all') {
31                 return;
32         }
33
34         if ($request_id) {
35                 /** @var Introduction $Intro */
36                 $Intro = \Friendica\BaseObject::getClass(Introduction::class);
37                 $Intro->fetch(['id' => $request_id, 'uid' => local_user()]);
38
39                 switch ($_POST['submit']) {
40                         case L10n::t('Discard'):
41                                 $Intro->discard();
42                                 break;
43                         case L10n::t('Ignore'):
44                                 $Intro->ignore();
45                                 break;
46                 }
47
48                 $a->internalRedirect('notifications/intros');
49         }
50 }
51
52 function notifications_content(App $a)
53 {
54         if (!local_user()) {
55                 notice(L10n::t('Permission denied.') . EOL);
56                 return Login::form();
57         }
58
59         $page = ($_REQUEST['page'] ?? 0) ?: 1;
60         $show = ($_REQUEST['show'] ?? '') === 'all';
61
62         Nav::setSelected('notifications');
63
64         $json = (($a->argc > 1 && $a->argv[$a->argc - 1] === 'json') ? true : false);
65
66         $nm = DI::notify();
67
68         $o = '';
69         // Get the nav tabs for the notification pages
70         $tabs = $nm->getTabs();
71         $notif_content = [];
72         $notif_nocontent = '';
73
74         // Notification results per page
75         $perpage = 20;
76         $startrec = ($page * $perpage) - $perpage;
77
78         $notif_header = L10n::t('Notifications');
79
80         $all = false;
81
82         // Get introductions
83         if ((($a->argc > 1) && ($a->argv[1] == 'intros')) || (($a->argc == 1))) {
84                 Nav::setSelected('introductions');
85
86                 $id = 0;
87                 if (!empty($a->argv[2]) && intval($a->argv[2]) != 0) {
88                         $id = (int)$a->argv[2];
89                 }
90
91                 $all = (($a->argc > 2) && ($a->argv[2] == 'all'));
92
93                 $notifs = $nm->getIntroList($all, $startrec, $perpage, $id);
94
95         // Get the network notifications
96         } elseif (($a->argc > 1) && ($a->argv[1] == 'network')) {
97                 $notif_header = L10n::t('Network Notifications');
98                 $notifs = $nm->getNetworkList($show, $startrec, $perpage);
99
100         // Get the system notifications
101         } elseif (($a->argc > 1) && ($a->argv[1] == 'system')) {
102                 $notif_header = L10n::t('System Notifications');
103                 $notifs = $nm->getSystemList($show, $startrec, $perpage);
104
105         // Get the personal notifications
106         } elseif (($a->argc > 1) && ($a->argv[1] == 'personal')) {
107                 $notif_header = L10n::t('Personal Notifications');
108                 $notifs = $nm->getPersonalList($show, $startrec, $perpage);
109
110         // Get the home notifications
111         } elseif (($a->argc > 1) && ($a->argv[1] == 'home')) {
112                 $notif_header = L10n::t('Home Notifications');
113                 $notifs = $nm->getHomeList($show, $startrec, $perpage);
114         // fallback - redirect to main page
115         } else {
116                 $a->internalRedirect('notifications');
117         }
118
119         // Set the pager
120         $pager = new Pager($a->query_string, $perpage);
121
122         // Add additional informations (needed for json output)
123         $notifs['items_page'] = $pager->getItemsPerPage();
124         $notifs['page'] = $pager->getPage();
125
126         // Json output
127         if (intval($json) === 1) {
128                 System::jsonExit($notifs);
129         }
130
131         $notif_tpl = Renderer::getMarkupTemplate('notifications.tpl');
132
133         $notif_show_lnk = [
134                 'href' => ($show ? 'notifications/' . $notifs['ident'] : 'notifications/' . $notifs['ident'] . '?show=all' ),
135                 'text' => ($show ? L10n::t('Show unread') : L10n::t('Show all')),
136         ];
137
138         // Process the data for template creation
139         if (($notifs['ident'] ?? '') == 'introductions') {
140                 $sugg = Renderer::getMarkupTemplate('suggestions.tpl');
141                 $tpl = Renderer::getMarkupTemplate('intros.tpl');
142
143                 // The link to switch between ignored and normal connection requests
144                 $notif_show_lnk = [
145                         'href' => (!$all ? 'notifications/intros/all' : 'notifications/intros' ),
146                         'text' => (!$all ? L10n::t('Show Ignored Requests') : L10n::t('Hide Ignored Requests'))
147                 ];
148
149                 // Loop through all introduction notifications.This creates an array with the output html for each
150                 // introduction
151                 foreach ($notifs['notifications'] as $notif) {
152
153                         // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
154                         // We have to distinguish between these two because they use different data.
155                         switch ($notif['label']) {
156                                 case 'friend_suggestion':
157                                         $notif_content[] = Renderer::replaceMacros($sugg, [
158                                                 '$type'       => $notif['label'],
159                                                 '$str_notifytype' => L10n::t('Notification type:'),
160                                                 '$notify_type'=> $notif['notify_type'],
161                                                 '$intro_id'   => $notif['intro_id'],
162                                                 '$lbl_madeby' => L10n::t('Suggested by:'),
163                                                 '$madeby'     => $notif['madeby'],
164                                                 '$madeby_url' => $notif['madeby_url'],
165                                                 '$madeby_zrl' => $notif['madeby_zrl'],
166                                                 '$madeby_addr'=> $notif['madeby_addr'],
167                                                 '$contact_id' => $notif['contact_id'],
168                                                 '$photo'      => $notif['photo'],
169                                                 '$fullname'   => $notif['name'],
170                                                 '$url'        => $notif['url'],
171                                                 '$zrl'        => $notif['zrl'],
172                                                 '$lbl_url'    => L10n::t('Profile URL'),
173                                                 '$addr'       => $notif['addr'],
174                                                 '$hidden'     => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
175                                                 '$knowyou'    => $notif['knowyou'],
176                                                 '$approve'    => L10n::t('Approve'),
177                                                 '$note'       => $notif['note'],
178                                                 '$request'    => $notif['request'],
179                                                 '$ignore'     => L10n::t('Ignore'),
180                                                 '$discard'    => L10n::t('Discard'),
181                                         ]);
182                                         break;
183
184                                 // Normal connection requests
185                                 default:
186                                         $friend_selected = (($notif['network'] !== Protocol::OSTATUS) ? ' checked="checked" ' : ' disabled ');
187                                         $fan_selected = (($notif['network'] === Protocol::OSTATUS) ? ' checked="checked" disabled ' : '');
188
189                                         $lbl_knowyou = '';
190                                         $knowyou     = '';
191                                         $helptext    = '';
192                                         $helptext2   = '';
193                                         $helptext3   = '';
194
195                                         if ($notif['network'] === Protocol::DFRN) {
196                                                 $lbl_knowyou = L10n::t('Claims to be known to you: ');
197                                                 $knowyou   = (($notif['knowyou']) ? L10n::t('yes') : L10n::t('no'));
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.', $notif['name'], $notif['name']);
200                                                 $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.', $notif['name']);
201                                         } elseif ($notif['network'] === Protocol::DIASPORA) {
202                                                 $helptext  = L10n::t('Shall your connection be bidirectional or not?');
203                                                 $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.', $notif['name'], $notif['name']);
204                                                 $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.', $notif['name']);
205                                         }
206
207                                         $dfrn_tpl = Renderer::getMarkupTemplate('netfriend.tpl');
208                                         $dfrn_text = Renderer::replaceMacros($dfrn_tpl, [
209                                                 '$intro_id'    => $notif['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'   => L10n::t('Friend'),
216                                                 '$as_fan'      => (($notif['network'] == Protocol::DIASPORA) ? L10n::t('Sharer') : L10n::t('Subscriber'))
217                                         ]);
218
219                                         $contact = DBA::selectFirst('contact', ['network', 'protocol'], ['id' => $notif['contact_id']]);
220
221                                         if (($contact['network'] != Protocol::DFRN) || ($contact['protocol'] == Protocol::ACTIVITYPUB)) {
222                                                 $action = 'follow_confirm';
223                                         } else {
224                                                 $action = 'dfrn_confirm';
225                                         }
226
227                                         $header = $notif['name'];
228
229                                         if ($notif['addr'] != '') {
230                                                 $header .= ' <' . $notif['addr'] . '>';
231                                         }
232
233                                         $header .= ' (' . ContactSelector::networkToName($notif['network'], $notif['url']) . ')';
234
235                                         if ($notif['network'] != Protocol::DIASPORA) {
236                                                 $discard = L10n::t('Discard');
237                                         } else {
238                                                 $discard = '';
239                                         }
240
241                                         $notif_content[] = Renderer::replaceMacros($tpl, [
242                                                 '$type'        => $notif['label'],
243                                                 '$header'      => $header,
244                                                 '$str_notifytype' => L10n::t('Notification type:'),
245                                                 '$notify_type' => $notif['notify_type'],
246                                                 '$dfrn_text'   => $dfrn_text,
247                                                 '$dfrn_id'     => $notif['dfrn_id'],
248                                                 '$uid'         => $notif['uid'],
249                                                 '$intro_id'    => $notif['intro_id'],
250                                                 '$contact_id'  => $notif['contact_id'],
251                                                 '$photo'       => $notif['photo'],
252                                                 '$fullname'    => $notif['name'],
253                                                 '$location'    => $notif['location'],
254                                                 '$lbl_location'=> L10n::t('Location:'),
255                                                 '$about'       => $notif['about'],
256                                                 '$lbl_about'   => L10n::t('About:'),
257                                                 '$keywords'    => $notif['keywords'],
258                                                 '$lbl_keywords'=> L10n::t('Tags:'),
259                                                 '$gender'      => $notif['gender'],
260                                                 '$lbl_gender'  => L10n::t('Gender:'),
261                                                 '$hidden'      => ['hidden', L10n::t('Hide this contact from others'), ($notif['hidden'] == 1), ''],
262                                                 '$url'         => $notif['url'],
263                                                 '$zrl'         => $notif['zrl'],
264                                                 '$lbl_url'     => L10n::t('Profile URL'),
265                                                 '$addr'        => $notif['addr'],
266                                                 '$lbl_knowyou' => $lbl_knowyou,
267                                                 '$lbl_network' => L10n::t('Network:'),
268                                                 '$network'     => ContactSelector::networkToName($notif['network'], $notif['url']),
269                                                 '$knowyou'     => $knowyou,
270                                                 '$approve'     => L10n::t('Approve'),
271                                                 '$note'        => $notif['note'],
272                                                 '$ignore'      => L10n::t('Ignore'),
273                                                 '$discard'     => $discard,
274                                                 '$action'      => $action,
275                                         ]);
276                                         break;
277                         }
278                 }
279
280                 if (count($notifs['notifications']) == 0) {
281                         info(L10n::t('No introductions.') . EOL);
282                 }
283
284                 // Normal notifications (no introductions)
285         } elseif (!empty($notifs['notifications'])) {
286                 // Loop trough ever notification This creates an array with the output html for each
287                 // notification and apply the correct template according to the notificationtype (label).
288                 foreach ($notifs['notifications'] as $notif) {
289                         $notification_templates = [
290                                 'like'        => 'notifications_likes_item.tpl',
291                                 'dislike'     => 'notifications_dislikes_item.tpl',
292                                 'attend'      => 'notifications_attend_item.tpl',
293                                 'attendno'    => 'notifications_attend_item.tpl',
294                                 'attendmaybe' => 'notifications_attend_item.tpl',
295                                 'friend'      => 'notifications_friends_item.tpl',
296                                 'comment'     => 'notifications_comments_item.tpl',
297                                 'post'        => 'notifications_posts_item.tpl',
298                                 'notify'      => 'notify.tpl',
299                         ];
300
301                         $tpl_notif = Renderer::getMarkupTemplate($notification_templates[$notif['label']]);
302
303                         $notif_content[] = Renderer::replaceMacros($tpl_notif, [
304                                 '$item_label' => $notif['label'],
305                                 '$item_link'  => $notif['link'],
306                                 '$item_image' => $notif['image'],
307                                 '$item_url'   => $notif['url'],
308                                 '$item_text'  => $notif['text'],
309                                 '$item_when'  => $notif['when'],
310                                 '$item_ago'   => $notif['ago'],
311                                 '$item_seen'  => $notif['seen'],
312                         ]);
313                 }
314         } else {
315                 $notif_nocontent = L10n::t('No more %s notifications.', $notifs['ident']);
316         }
317
318         $o .= Renderer::replaceMacros($notif_tpl, [
319                 '$notif_header'    => $notif_header,
320                 '$tabs'            => $tabs,
321                 '$notif_content'   => $notif_content,
322                 '$notif_nocontent' => $notif_nocontent,
323                 '$notif_show_lnk'  => $notif_show_lnk,
324                 '$notif_paginate'  => $pager->renderMinimal(count($notif_content))
325         ]);
326
327         return $o;
328 }