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