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