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