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