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