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