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